WPF C#绑定代码 - 为什么这个简单的例子不起作用?

时间:2010-09-08 05:54:00

标签: c# .net wpf binding

我附上了一些WPF C#绑定代码 - 为什么这个简单的例子不起作用? (只是试图理解对自定义对象的绑定)。也就是说,单击按钮以增加模型中的计数器时,标签不会更新。

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

}

感谢

3 个答案:

答案 0 :(得分:4)

对于这个简单的例子,考虑使用INotifyPropertyChanged而不是DependencyProperties!

<强>更新 如果您确实要使用DP,请使用VS2010中的propdp代码段或Dr WPF's snippets for VS2008

答案 1 :(得分:3)

TestCounter需要是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));

答案 2 :(得分:1)

您可以在System.ComponentModel命名空间中实现INotifyPropertyChanged接口。我通常实现一个Changed方法,它可以获取许多属性名称并检查未设置的事件。我这样做是因为有时我有多个依赖于一个值的属性,我可以从我的所有属性设置器中调用一个方法。

例如,如果你有一个带有Width和Height属性的Rectangle类和一个返回Width * Height的Area只读属性,你可以放置Changed(“Width”,“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));
            }
        }
    }
}