Answer the question
In order to leave comments, you need to log in
How to display value in Label using WPF?
I'm new to C#, and to programming in general (I program in Pascal at school, but it doesn't count). I decided to make a small program where the user enters 2 numbers from the keyboard, and then also enters the operation that he wants to perform with these numbers. After clicking the "Result" button, the value of the Z variable should be displayed in the Label.
Having written the following code, I was puzzled:
public int Z { get; set; }
public int X { get; set; }
public int Y { get; set; }
public string Operation { get; set; }
public MainWindow()
{
InitializeComponent();
}
private void TextBox_TextChanged_1(object sender, TextChangedEventArgs e)
{
}
private void TextBox_TextChanged_2(object sender, TextChangedEventArgs e)
{
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
switch (Operation)
{
case "+":
Z = X + Y;
break;
case "-":
Z = X - Y;
break;
case "*":
Z =X * Y;
break;
case "/":
Z = X / Y;
break;
}
}
<TextBox x:Name="TextBoxX"
HorizontalAlignment="Left"
Height="16"
TextWrapping="Wrap"
VerticalAlignment="Top"
Width="124"
Margin="53.5,83,0,0"
Background="#FF232121"
BorderBrush="{x:Null}"
Grid.Column="1"
Grid.ColumnSpan="2"
Foreground="White"
TextChanged="TextBox_TextChanged_1"
Text ="{Binding X}"
TextInput="TextBoxX_TextInput"/>
Answer the question
In order to leave comments, you need to log in
You can simply do in the constructor
DataContext = this;
Then for your Window to implement INotifyPropertyChanged (possibly a mistake in the name)
Further, for example, for Z
private int _z;
public int Z {
get => _z;
set{
_z = value;
OnPropertyChanged(nameof(Z));
} }
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question