代码之家  ›  专栏  ›  技术社区  ›  Greg

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

  •  3
  • Greg  · 技术社区  · 14 年前

    <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 回复  |  直到 12 年前
        1
  •  3
  •   Gennady Vanin Геннадий Ванин Mikael Svenson    12 年前

        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
  •  4
  •   rudigrobler    14 年前

    对于这个简单的示例,请考虑使用INotifyPropertyChanged而不是dependencProperties!

    更新 如果确实要使用DPs,请使用VS2010或 Dr WPF's snippets for VS2008 ?

        3
  •  1
  •   Jason Goemaat    14 年前

    可以在中实现INotifyPropertyChanged接口系统组件模型命名空间。我通常实现一个更改过的方法,该方法可以接受许多属性名并检查未设置的事件。我这样做是因为有时我有多个依赖于一个值的属性,我可以从所有属性设置器中调用一个方法。

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