我是新手WPF,我试图完成一个我认为很简单的任务 - 在我的程序运行过程中显示业务对象字段的值随着变化而变化。我知道如何通过在C#中手动更改TextBox.Text属性来“强制”更改它,但是由于我正在学习WPF,我想用“正确”的方式做,这意味着使用数据绑定。
问题#1:据我所知,我的选择是使用DependencyProperty或在我的业务对象中实现INotifyPropertyChanged,对吗?
问题#2:这是我尝试使用DependencyProperty路线的通用代码版本。
标记:
Button x:Name="nextButton" Content="Click" Grid.Row="2" Click="nextButton_Click" />
TextBox x:Name="myTextBox" Grid.Row="1" Text="{Binding Source=myTest, Path=Name, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True}"/>
代码后端:
namespace DependencyTest2
{
///
/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow : Window
{
private int i;
private TestSphere myTest;
public MainWindow()
{
InitializeComponent();
i = 0;
myTest = new TestSphere();
}
private void nextButton_Click(object sender, RoutedEventArgs e)
{
switch (i)
{
case 0:
myTest.Name = "string1";
break;
case 1:
myTest.Name = "string2";
break;
case 2:
myTest.Name = "string3";
break;
}
i++;
}
}
class TestSphere : DependencyObject
{
public string Name
{
get { return (string)GetValue(NameProperty); }
set { SetValue(NameProperty, value); }
}
public static readonly DependencyProperty NameProperty =
DependencyProperty.Register("Name", typeof(string), typeof(TestSphere));
public TestSphere()
{
Name = "default";
}
}
}
当我运行程序时,文本框中没有任何内容显示,即使绑定属性有一个值 - 我需要做些什么来通知绑定源值已更改吗?我认为使用DependencyProperty作为源应该会处理这个问题,但再次说明,我是一个WPF新手。谢谢!
- 史蒂夫
class TestSphere : NotifyProperyChangedBase
{
private string _Name;
public string Name
{
get { return _Name; }
set
{
this.CheckPropertyChanged("Name", ref _Name, ref value);
}
}
public TestSphere()
{
Name = "default";
}
}
public abstract class NotifyProperyChangedBase : INotifyPropertyChanged
{
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
#region methods
protected bool CheckPropertyChanged(string propertyName, ref T oldValue, ref T newValue)
{
if (oldValue == null && newValue == null)
{
return false;
}
if ((oldValue == null && newValue != null) || !oldValue.Equals((T)newValue))
{
oldValue = newValue;
FirePropertyChanged(propertyName);
return true;
}
return false;
}
protected void FirePropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
这是我的新标记语言:
Grid Name="myGrid">
Grid.RowDefinitions>
RowDefinition Height="30"/>
RowDefinition Height="30"/>
RowDefinition Height="30"/>
RowDefinition Height="*"/>
/Grid.RowDefinitions>
Label x:Name="myLabel" Grid.Row="0" Foreground="Black" />
Button x:Name="nextButton" Content="Click" Grid.Row="2" Click="nextButton_Click" />
TextBox x:Name="myTextBox" Grid.Row="1" Text="{Binding Path=myTest.Name}"/>
/Grid>
在我实例化myTest之后,我还添加了一行代码myGrid.DataContext = myTest;
到'public MainWindow()'中。当我逐步执行生成的程序时,this.PropertyChanged
的值总是评估为null
,因此PropertyChanged事件从未触发。提前抱歉,这一定是一个非常初级的问题。
- 史蒂夫
Binding Path=Name
而不是Binding Path=myTest.Name
就解决了!非常感谢您的帮助! - seveland