我想在一个类实例和两个 WPF 文本框之间设置一个绑定。尽管如此,文本框并没有改变它们的状态,我不知道我做错了什么。
XAML
<DockPanel>
<TextBlock Style="{StaticResource caption}">Testing System</TextBlock>
<TextBlock DockPanel.Dock="Left" x:Name="txt1" Text="DC"/>
<TextBlock DockPanel.Dock="Right" x:Name="txt2" Text="PD"/>
<Button Height="20" Width="100" Click="clickBinding">Bind</Button>
<Button Height="20" Width="100" Click="clickChangeBinding">Change Status</Button>
</DockPanel>
主窗口.xaml.cs
private ADSbinding myADS = new ADSbinding();
private void clickBinding(object sender, RoutedEventArgs e)
{
Binding b1, b2;
b1 = new Binding();
b2 = new Binding();
b1.Source = myADS.DeviceConfigured;
b2.Source = myADS.ProcessingData;
b1.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
b2.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
BindingOperations.SetBinding(txt1, TextBlock.TextProperty, b1);
BindingOperations.SetBinding(txt2, TextBlock.TextProperty, b2);
}
private void clickChangeBinding(object sender, RoutedEventArgs e)
{
myADS.changedata();
}
班级:
public class ADSbinding : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string deviceConfigured = "false";
private string processingData = "false";
public ADSbinding()
{
ProcessingData = "true";
}
// Get-Set methods
public string DeviceConfigured
{
get { return deviceConfigured; }
set
{
deviceConfigured = value;
Changed("DeviceConfigured");
}
}
public string ProcessingData
{
get { return processingData; }
set
{
processingData = value;
Changed("ProcessingData");
}
}
private void Changed(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public void changedata()
{
DeviceConfigured = "change";
ProcessingData = "change";
}
}
当按下“clickBinding”时,状态会改变,当“clickChangeBinding”它保持不变时,再次点击“clickBinding”它会改变。这是一个非常直接的尝试,我无法弄清楚问题出在哪里。任何人?