您好,
>>在记录数改变的时候,不起作用,初始化的时候起作用了,难道与INotifyPropertyChanged接口有关系?
第一次设置绑定时,绑定是成功的,按钮会被禁止,之后对myList添加Item后,是不会通知Button的绑定更新数据的,需要实现 INotifyPropertyChanged 接口。
对于您的代码,可以在对myList更新后,重新设置一次绑定,即可更新Button的状态:
public MainWindow()
{
InitializeComponent();
setBinding();
}
private void setBinding()
{
bMod.SetBinding(Button.IsEnabledProperty, new Binding("Count")
{
Source = myList,
Mode = BindingMode.OneWay,
Converter = new IntToBooleanConverter()
});
tb1.SetBinding(TextBlock.TextProperty, new Binding("Count") { Source = myList });
}
private void Button_Click(object sender, RoutedEventArgs e)
{
myList.Add(new Customer() { ID = "1", Name = "Test" });
setBinding();
}
XAML:
<Grid>
<TextBlock Name="tb1" FontWeight="Bold" FontSize="20" />
<StackPanel VerticalAlignment="Center" Orientation="Horizontal">
<Button Name="bMod" Margin="50" Width="100" Height="50" Content="TestButton" />
<Button Content="Click" Margin="50" Width="100" Height="50" Click="Button_Click" />
</StackPanel>
</Grid>

请阅读以下参考资料:
#How to: Create a Binding in Code
http://msdn.microsoft.com/en-us/library/ms742863.aspx
<THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
Thanks
MSDN Community Support
Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.