F
F
Floydreme2018-03-14 22:30:54
WPF
Floydreme, 2018-03-14 22:30:54

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.
5aa976fd5931d316260408.png
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;
            }

        }

Element binding in XAML looks like this:
<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"/>

How to make Z appear in Label? And if there are various comments, I will be glad to listen - it is useful for me.
PS - Don't be angry for various crutches :)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Simonov, 2018-03-15
@FloydReme

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));
} }

And bind
Not very beautiful, but in order not to bother and just use some things in the vpf it will do, and in the future you really need to look at mvvm and put it all in the ViewModel

L
leremin, 2018-03-14
@leremin

These properties should not be in the form class, but in a separate class that needs to be assigned as the form's DataContext.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question