WPF return value from checkbox unchecked event - c#

How can I return a value from when check-box is unchecked? I noticed that you need to create a converter but is there an easer way of doing that?
XAML
<Grid>
<TextBox x:Name="textBox4" Visibility="Hidden" HorizontalAlignment="Left" Height="23" Margin="149,135,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="43" TextChanged="textBox4_TextChanged"/>
<CheckBox x:Name="checkBox" Content="Party?(4 or more)" HorizontalAlignment="Left" Margin="33,135,0,0" VerticalAlignment="Top" Checked="checkBox_Checked" Width="116" Height="23"/>
</Grid>
C#
private void checkBox_Checked(object sender, RoutedEventArgs e)
{
Handle(sender as CheckBox);
}
private void checkBox_Unchecked(object sender, RoutedEventArgs e)
{
Handle(sender as CheckBox);
}
void Handle(CheckBox checkBox)
{
bool chkd = checkBox.IsChecked.Value;
if (chkd)
{
textBox4.Visibility = Visibility.Visible;
textBox6.IsEnabled = IsEnabled.Equals(false);
}
else
{
textBox4.Visibility = Visibility.Hidden;
}

Add a handler for the Unchecked event in your XAML, and your code will get called.
Snippet:
Unchecked="checkBox_Unchecked"
Complete:
<CheckBox x:Name="checkBox" Content="Party?(4 or more)" HorizontalAlignment="Left" Margin="33,135,0,0" VerticalAlignment="Top" Checked="checkBox_Checked" Unchecked="checkBox_Unchecked" Width="116" Height="23"/>
When working with controls/events and to help discover what is available, see the Properties window and Events as in the below image:

Related

My first project using C# & WPF - how to set a variable from .xaml.cs to appear in the textbox?

I'm trying to build a calculator with C# and WPF.
The purpose is to learn and test new things.
Here's the code:
XAML
<Window x:Class="calculator.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:calculator"
mc:Ignorable="d"
Title="MainWindow" Height="400" Width="400">
<Grid>
<Button Content="9" HorizontalAlignment="Left" Margin="295,224,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="Button_Click9"/>
<Button Content="8" HorizontalAlignment="Left" Margin="166,224,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="Button_Click8"/>
<Button Content="7" HorizontalAlignment="Left" Margin="37,224,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="Button_Click7"/>
<Button Content="6" HorizontalAlignment="Left" Margin="295,254,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="Button_Click6"/>
<Button Content="5" HorizontalAlignment="Left" Margin="166,254,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="Button_Click5"/>
<Button Content="4" HorizontalAlignment="Left" Margin="37,254,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="Button_Click4"/>
<Button Content="3" HorizontalAlignment="Left" Margin="295,285,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="Button_Click3"/>
<Button Content="2" HorizontalAlignment="Left" Margin="166,285,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="Button_Click2"/>
<Button Content="1" HorizontalAlignment="Left" Margin="37,285,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="Button_Click1"/>
<Button Content="0" HorizontalAlignment="Left" Margin="37,316,0,0" VerticalAlignment="Top" Width="321" FontFamily="Arial" Click="Button_Click0"/>
<TextBox HorizontalAlignment="Left" Height="35" Margin="37,49,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="321" FontFamily="Arial" FontSize="20" TextAlignment="Right"/>
<Button Content="Calculate!" HorizontalAlignment="Left" Margin="154,10,0,0" VerticalAlignment="Top" Width="75"/>
<Button Content="+" HorizontalAlignment="Left" Margin="37,125,0,0" VerticalAlignment="Top" Width="68" Height="57" Click="Button_Click_Add"/>
<Button Content="/" HorizontalAlignment="Left" Margin="290,125,0,0" VerticalAlignment="Top" Width="68" Height="57" Click="Button_Click_Multiply"/>
<Button Content="*" HorizontalAlignment="Left" Margin="206,125,0,0" VerticalAlignment="Top" Width="69" Height="57" Click="Button_Click_Divide"/>
<Button Content="-" HorizontalAlignment="Left" Margin="122,125,0,0" VerticalAlignment="Top" Width="68" Height="57" Click="Button_Click_Subtract"/>
</Grid>
</Window>
XAML.CS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace calculator
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
List<int> lista = new List<int>();
string number = "";
int res = 0;
private void Button_Click_Add(object sender, RoutedEventArgs e)
{
int numberCreated;
try
{
numberCreated = Int32.Parse(number);
lista.Add(numberCreated);
}
catch (Exception)
{
MessageBox.Show("Number could not be added!");
}
number = "";
if(lista.Count != 0)
{
foreach(var element in lista)
{
res += element;
}
lista.Clear();
}
MessageBox.Show(res.ToString());
}
private void Button_Click_Subtract(object sender, RoutedEventArgs e)
{
try
{
int numberCreated = Int32.Parse(number);
lista.Add(numberCreated);
}
catch(Exception)
{
MessageBox.Show("Number could not be added!");
}
number = "";
if (lista.Count != 0)
{
foreach (var element in lista)
{
res -= element;
}
lista.Clear();
}
MessageBox.Show(res.ToString());
}
private void Button_Click_Multiply(object sender, RoutedEventArgs e)
{
try
{
int numberCreated = Int32.Parse(number);
lista.Add(numberCreated);
}
catch (Exception)
{
MessageBox.Show("Number could not be added!");
}
number = "";
if (lista.Count != 0)
{
foreach (var element in lista)
{
res *= element;
}
lista.Clear();
}
MessageBox.Show(res.ToString());
}
private void Button_Click_Divide(object sender, RoutedEventArgs e)
{
try
{
int numberCreated = Int32.Parse(number);
lista.Add(numberCreated);
}
catch (Exception)
{
MessageBox.Show("Number could not be added");
}
number = "";
if (lista.Count != 0)
{
foreach (var element in lista)
{
res /= element;
}
lista.Clear();
}
MessageBox.Show(res.ToString());
}
private void Button_Click9(object sender, RoutedEventArgs e)
{
number += "9";
}
private void Button_Click8(object sender, RoutedEventArgs e)
{
number += "8";
}
private void Button_Click7(object sender, RoutedEventArgs e)
{
number += "7";
}
private void Button_Click6(object sender, RoutedEventArgs e)
{
number += "6";
}
private void Button_Click5(object sender, RoutedEventArgs e)
{
number += "5";
}
private void Button_Click4(object sender, RoutedEventArgs e)
{
number += "4";
}
private void Button_Click3(object sender, RoutedEventArgs e)
{
number += "3";
}
private void Button_Click2(object sender, RoutedEventArgs e)
{
number += "2";
}
private void Button_Click1(object sender, RoutedEventArgs e)
{
number += "1";
}
private void Button_Click0(object sender, RoutedEventArgs e)
{
number += "0";
}
}
}
I'm open to any tips and ideas you might give to an absolute beginner.
The reason I'm posting this is because I don't know how to make the "res" variable appear in the TextBox after each operation.
I saw online many people talking about TextBox.text, but I don't know how to use it.
Probably I have to use it in .xaml.cs, but how can I define TextBox? How will my program know which TextBox I want the text to be applied to?
Also, I feel like there's a better approach to create a number using the buttons rather than creating 10 separate functions (from button_click0 to button_click9) and I'd like to hear some ideas.
Thanks!
There are several ways to do this, e.g. Data Binding.
But I guess for learning, since this is your very first step in WPF, you should start more easy.
To access a control via code, you have to give it a name in XAML:
<TextBox x:Name="TxtResult" HorizontalAlignment="Left" Height="35" Margin="37,49,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="321" FontFamily="Arial" FontSize="20" TextAlignment="Right"/>
By that, you can access your Textbox in Code:
private void Button_Click9(object sender, RoutedEventArgs e)
{
number += "9";
this.TxtResult.Text = number;
}
EDIT to answer your last question:
Also for this there are many ways how to make it better or easier. If you want, you can do some research about Commands, Command Parameters and Command Bindings.
But for this simple Demo case, you can simply use the same event handler for all number buttons:
private void ButtonNumberClick(object sender, RoutedEventArgs e)
{
number += ((Button) sender).Content;
this.TxtResult.Text = number;
}
The sender parameter contains the UI object that triggered the event handler, so you'll get a reference to the button that was clicked. And since the Content property of each button already contains the number you want to add to your string, you can grab it from there and append it to the number string.
Now in XAML you just have to assign that event handler to all ten buttons:
<Button Content="9" HorizontalAlignment="Left" Margin="295,224,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="ButtonNumberClick"/>
<Button Content="8" HorizontalAlignment="Left" Margin="166,224,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="ButtonNumberClick"/>
<Button Content="7" HorizontalAlignment="Left" Margin="37,224,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="ButtonNumberClick"/>
<Button Content="6" HorizontalAlignment="Left" Margin="295,254,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="ButtonNumberClick"/>
<Button Content="5" HorizontalAlignment="Left" Margin="166,254,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="ButtonNumberClick"/>
<Button Content="4" HorizontalAlignment="Left" Margin="37,254,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="ButtonNumberClick"/>
<Button Content="3" HorizontalAlignment="Left" Margin="295,285,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="ButtonNumberClick"/>
<Button Content="2" HorizontalAlignment="Left" Margin="166,285,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="ButtonNumberClick"/>
<Button Content="1" HorizontalAlignment="Left" Margin="37,285,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="ButtonNumberClick"/>
<Button Content="0" HorizontalAlignment="Left" Margin="37,316,0,0" VerticalAlignment="Top" Width="321" FontFamily="Arial" Click="ButtonNumberClick"/>

send data from one window to another c# xaml wpf

i want to send Data from one Textbox on window One to a label on window Two.
starting with window two:
<StackPanel>
<StackPanel x:Name="ButtonStackPanel" Height="Auto" Width="Auto">
<StackPanel Orientation="Horizontal">
<StackPanel Orientation="Vertical">
<Button Style="{DynamicResource ButtonStyle}" Content="To The Dark Side" Click="OnClickToDarkSide"/>
<Button Style="{DynamicResource ButtonStyle}" Content="To The Gray" Click="OnClickToGraySide"/>
<Button Style="{DynamicResource ButtonStyle}" Content="To The Light Side" Click="OnClickToLightSide"/>
</StackPanel>
<Border HorizontalAlignment="Center" VerticalAlignment="Stretch" Background="Red" Height="Auto" Width="2"/>
<Label Style="{DynamicResource LabelStyle}" x:Name="theTextBlock" Content="{Binding Source=CodeText}"/>
<Border HorizontalAlignment="Center" VerticalAlignment="Stretch" Background="Red" Height="Auto" Width="2"/>
<ToggleButton Style="{DynamicResource ToggleButtonStyle}" Content="Open Style Window" Name="StyleWindowButton" Click="OnClickOpenStyleWindow"/>
<ToggleButton Style="{DynamicResource ToggleButtonStyle}" Content="Open Text Window" Name="TextWindowButton" Click="OnClickOpenTextWindow"/>
</StackPanel>
<Border Height="2" Width="Auto" Background="Red" HorizontalAlignment="Stretch" VerticalAlignment="Center"/>
</StackPanel>
<Border Height="2" Width="Auto" Background="Red" HorizontalAlignment="Stretch" VerticalAlignment="Center"/>
</StackPanel>
Codebehind of Window Two:
public MainWindow()
{
(App.Current as App).CodeText = _jediCode;
InitializeComponent();
}
private void OnClickToDarkSide(object sender, RoutedEventArgs e)
{
(App.Current as App).ChangeSkin(Skin.Dark);
(App.Current as App).CodeText = _sithCode;
theTextBlock.Content = (App.Current as App).CodeText;
}
private void OnClickToLightSide(object sender, RoutedEventArgs e)
{
(App.Current as App).ChangeSkin(Skin.Light);
(App.Current as App).CodeText = _jediCode;
theTextBlock.Content = (App.Current as App).CodeText;
}
private void OnClickToGraySide(object sender, RoutedEventArgs e)
{
(App.Current as App).ChangeSkin(Skin.Gray);
(App.Current as App).CodeText = _grayCode;
theTextBlock.Content = (App.Current as App).CodeText;
}
private void OnClickOpenStyleWindow(object sender, RoutedEventArgs e)
{
if (StyleWindowButton.IsChecked == true)
{
styleWindow = new StyleWindow();
styleWindow.Show();
}
else
{
styleWindow.Close();
styleWindow = null;
}
}
private void OnClickOpenTextWindow(object sender, RoutedEventArgs e)
{
if (TextWindowButton.IsChecked == true)
{
textWindow = new InputWindow();
textWindow.Show();
}
else
{
textWindow.Close();
textWindow = null;
}
}
}
window one:
<Grid>
<TextBox HorizontalAlignment="Left" VerticalAlignment="Top" Height="200" Width="200" TextWrapping="Wrap"
AcceptsReturn="True" AcceptsTab="True" Text="{Binding Path=CodeText, Source={x:Static Application.Current}, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Margin="10,10,0,0">
<!-- TODO: trigger change from this textbox to the textblock in mainwindow -->
</TextBox>
</Grid>
code behind of one is empty.
app.xaml.cs:
public string CodeText
{
get => _codeText;
set { _codeText = value; OnPropertyChanged(nameof(CodeText)); }
}
Ok, the current behavior is clicking on one of the buttons (Dark Side, Gray, Light Side) leads to changes in the CodeText Property, which leads to a change of the content of the label of Window Two and the text of TextBox of Window One. Changing the text of the TextBox, changes also the CodeText Property, but does not lead to a change in the label and thats confusing, why does it work the one way, but not the other.
hope you have a hint for me. :) Maybe i missed a trigger or a kind of refresh for the label
bindings in window One and Two are set differently. (window Two does it wrong, Content="{Binding Source=CodeText}" is not valid binding)
in fact, window Two removes the binding by assigning CodeText directly as local value:
theTextBlock.Content = (App.Current as App).CodeText;
you should remove that line, and use the same binding as in window One:
<Label Style="{DynamicResource LabelStyle}" x:Name="theTextBlock"
Content="{Binding Path=CodeText, Source={x:Static Application.Current}}"/>

Button in a different file than the main

I have 2 button but they need to be in a different file than the mainWindow.cs.
I can't figure how to do that.
So the Button_Click_2 must be in the ReadData.cs and the
Button_Click_3 must be in the WriteData.cs
The app don't recognize the button when they are not in the mainWindow.
How can I do that ?
ReadData.cs :
public new void Button_Click_2(object sender, RoutedEventArgs e)
{
string text = verifyCard("5"); // 5 - is the block we are reading
textBlock1.Text = text.ToString();
}
MainWindow.xaml.cs :
public void Button_Click_2(object sender, RoutedEventArgs e)
{
//When I click it don't detected the code in the ReadData.cs
// The code of the button must be in the ReadData.cs
}
MainWindow.xaml :
<Grid>
<Button Content="Connexion" HorizontalAlignment="Left"
Margin="265,142,0,0" VerticalAlignment="Top" Width="236" Height="44"
Click="Button_Click_1"/>
<Button Content="Lire donnée carte" HorizontalAlignment="Left"
Margin="265,276,0,0" VerticalAlignment="Top" Width="236" Height="42"
Click="Button_Click_2"/>
<Button Content="Ecrire donnée carte" HorizontalAlignment="Left"
Margin="265,344,0,0" VerticalAlignment="Top" Width="236" Height="41"
Click="Button_Click_3"/>
<TextBox Name="textBox1" HorizontalAlignment="Left" Height="23"
Margin="321,224,0,0" TextWrapping="Wrap" Text="TextBox"
VerticalAlignment="Top" Width="120" TextChanged="TextBox_TextChanged"/>
<TextBlock Name="textBlock1" HorizontalAlignment="Left"
Margin="291,95,0,0" TextWrapping="Wrap" VerticalAlignment="Top"
Height="23" Width="177"/>
</Grid>
It must be possible to forced the app to detected and execute the code in another file than the Main.
I'm stuck... do any of you have the solution ?
You can't just place the button code in ReadData.cs class. as the event is related to ui of MainWindow.xaml create an object for ReadData do something like this
public new void Button_Click_2(object sender, RoutedEventArgs e)
{
ReadData rd= new ReadData();
string text = rd.verifyCard("5");
textBlock1.Text = text.ToString();
}

Get content of Current Button

<Button Name="btn1" Content="1" HorizontalAlignment="Left" Margin="13,127,0,0" VerticalAlignment="Top" Click="Button_Click"/>
<Button Name="btn2" Content="2" HorizontalAlignment="Left" Margin="135,124,0,0" VerticalAlignment="Top" Click="Button_Click"/>
How do i get the content of the current button with just one function call?
try this:
private void Button_Click(object sender, RoutedEventArgs e)
{
Button obj = (Button)sender;
MessageBox.Show(obj.Content.ToString());
}

GridView is Not Loading Data

I'm trying to load data into a GridView after a TextBlock from another GridView on the page has been tapped/clicked. The first GridView containing the list of TextBlocks loads correctly.
Here is my XAML code for both GridViews, my Bindings seem to be correct:
<GridView x:Name="CourseNoGridView" Margin="50,50,0,0" Grid.Row="1" VerticalAlignment="Top" Height="568" ItemsSource="{Binding Distinct_CourseNo}" SelectionMode="Single" Padding="0,0,0,10" HorizontalAlignment="Left" Width="525" SelectionChanged="CourseNoGridView_SelectionChanged">
<GridView.ItemTemplate>
<DataTemplate>
<Border BorderBrush="White">
<TextBlock x:Name="CourseNoTextBlock" Text="{Binding CourseNo}" TextWrapping="NoWrap" FontSize="24" Width="200" Height="Auto" Padding="10" Tapped="CourseNoTextBlock_Tapped"/>
</Border>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
<GridView x:Name="SectionsGridView" Margin="580,50,0,0" Grid.Row="1" VerticalAlignment="Top" Height="568" ItemsSource="{Binding Clicked_CourseNo_Sections}" SelectionMode="Single" Padding="0,0,0,10" HorizontalAlignment="Left" Width="776" SelectionChanged="CourseNoGridView_SelectionChanged">
<GridView.ItemTemplate>
<DataTemplate>
<Border BorderBrush="White">
<TextBlock x:Name="SectionTextBlock" Text="{Binding Get_Section}" TextWrapping="NoWrap" FontSize="24" Width="200" Height="Auto" Padding="10"/>
</Border>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
Here is my code for handling the clicking/tapping of an item in the first GridView:
private void CourseNoGridView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
clickedSection = (Sections)e.AddedItems[0];
}
private void CourseNoTextBlock_Tapped(object sender, TappedRoutedEventArgs e)
{
this.Clicked_CourseNo_Sections = (from s in Roster_Sections
where s.CourseNo.Equals(clickedSection.CourseNo)
select s).ToList();
}
What you want to do is use an ObservableCollection and bind your your Grid View to this. Then in your "Tapped" event handler you clear the existing items from this collection and add the new items.
Something like this:
private readonly ObservableCollection<Sections> currentSections = new ObservableCollection<Sections>();
//This is what we bind to
public ObservableCollection<Sections> CurrentSections { get { return currentSections; } }
private void CourseNoGridView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
clickedSection = (Sections)e.AddedItems[0];
}
private void CourseNoTextBlock_Tapped(object sender, TappedRoutedEventArgs e)
{
var courseSections = (from s in Roster_Sections
where s.CourseNo.Equals(clickedSection.CourseNo)
select s);
CurrentSections.Clear();
CurrentSections.AddRange(courseSections);
}
There's some documentation here:
http://msdn.microsoft.com/en-us/library/windows/apps/hh758320.aspx
It seems like adding the last line of code below fixed the problem.
private void CourseNoTextBlock_Tapped(object sender, TappedRoutedEventArgs e)
{
this.Clicked_CourseNo_Sections = (from s in Roster_Sections
where s.CourseNo.Equals(clickedSection.CourseNo)
select s).ToList();
SectionsGridView.ItemsSource = Clicked_CourseNo_Sections;
}

Categories

Resources