【发布时间】:2015-04-16 12:22:29
【问题描述】:
我正在尝试将延迟属性用于 WPF 绑定。下面的示例中有两个文本框绑定到同一个属性。第一个使用延迟属性,第二个不使用。
延迟效果很好。但是出乎意料的行为是,更改 TextBox1 中的值不会立即启用 Button,但 TextBox2 会。鼠标单击、输入键或使用 Tab 键离开文本框即可启用该按钮。
有人知道我该如何解决这个问题或原因是什么?
查看:
<Window x:Class="WpfApplication1.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">
<StackPanel>
<TextBox x:Name="TextBox1" Text="{Binding Value1, UpdateSourceTrigger=PropertyChanged, Delay=1000}"/>
<TextBox x:Name="TextBox2" Text="{Binding Value1, UpdateSourceTrigger=PropertyChanged}"/>
<Button Command="{Binding ButtonCommand}" Content="GO!"></Button>
</StackPanel>
代码隐藏:
public partial class MainWindow : Window, INotifyPropertyChanged
{
private const decimal InitialValue = 400;
private decimal _value1;
public decimal Value1
{
get { return _value1; }
set
{
_value1 = value;
OnPropertyChanged();
}
}
public ICommand ButtonCommand { get; set; }
public MainWindow()
{
InitializeComponent();
Value1 = InitialValue;
ButtonCommand = new RelayCommand(x => { /*Do something*/ }, x => Value1 != InitialValue);
DataContext = this;
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
【问题讨论】: