C # WPF Binding Code - Why Does This Simple Example Not Work?

I attached WPF binding code C # - why does this simple example not work? (just trying to understand the binding to a user object). That is, when you click on the button to increase the counter in the model, the label is not updated.

<Window x:Class="testapp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button  Height="23" HorizontalAlignment="Left" Margin="20,12,0,0" 
                 Name="testButton" VerticalAlignment="Top" Width="126" 
                 Click="testButton_Click" Content="Increase Counter" />
        <Label Content="{Binding Path=TestCounter}" Height="37" 
               HorizontalAlignment="Right" Margin="0,12,122,0" 
               Name="testLabel2" VerticalAlignment="Top" 
               BorderThickness="3" MinWidth="200"  />
    </Grid>
</Window>


namespace testapp1
{
    public partial class MainWindow : Window
    {
        public TestModel _model;

        public MainWindow()
        {
            InitializeComponent();

            InitializeComponent();
            _model = new TestModel();
            _model.TestCounter = 0;
            this.DataContext = _model;
        }

        private void testButton_Click(object sender, RoutedEventArgs e)
        {
            _model.TestCounter = _model.TestCounter + 1;
            Debug.WriteLine("TestCounter = " + _model.TestCounter);
        }
    }

    public class TestModel : DependencyObject
    {
        public int TestCounter { get; set; }
    }

}

thank

+3
source share
3 answers

TestCounter must be DepenencyProperty

    public int TestCounter
    {
        get { return (int)GetValue(TestCounterProperty); }
        set { SetValue(TestCounterProperty, value); }
    }

    // Using a DependencyProperty as the backing store for TestCounter.  
    //This enables animation, styling, binding, etc...
    public static readonly DependencyProperty TestCounterProperty =
        DependencyProperty.Register
             ("TestCounter", 
              typeof(int), 
              typeof(TestModel), 
              new UIPropertyMetadata(0));
+3
source

For this simple example, consider using the INotifyPropertyChanged and Not DependencyProperties properties!

UPDATE If you want to use DP, use the propdp fragment in VS2010 or the Dr WPF fragments for VS2008 ?

+4

INotifyPropertyChanged System.ComponentModel. Changed, , . , , , .

, Rectangle Width Height Area , Width * Height, Changed ( "", "Area"); Width.

public class TestModel : INotifyPropertyChanged
{
    int m_TestCounter;
    public int TestCounter {
        get {
            return m_TestCounter;
        }
        set {
            m_TestCounter = value;
            Changed("TestCounter");
        }
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion

    void Changed(params string[] propertyNames)
    {
        if (PropertyChanged != null)
        {
            foreach (string propertyName in propertyNames)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
+1

Source: https://habr.com/ru/post/1763727/


All Articles