我的WPF(弹出(控件中有两个按钮。
第一个是取消按钮,它只是关闭弹出窗口并始终启用。
第二个是"确定"按钮,仅当三个(绑定(文本框中的一个发生更改时,才应启用该按钮。
问题是,在文本框失去焦点之前,绑定属性的值不会更改。 因此,该按钮直到那时才处于活动状态,因此无法使用鼠标单击。
<TextBox x:Name="tbFunction" Text="{Binding FunctionSign}" Grid.Column="1" Grid.Row="1" VerticalContentAlignment="Center" Margin="2" Padding="10 0 0 0"/>
<TextBox x:Name="tbSpecs" Text="{Binding Specs}" Grid.Column="1" Grid.Row="1" VerticalContentAlignment="Center" Margin="2" Padding="10 0 0 0"/>
<TextBox x:Name="tbSupplement" Text="{Binding Supplement}" Grid.Column="1" Grid.Row="1" VerticalContentAlignment="Center" Margin="2" Padding="10 0 0 0"/>
<!-- Footer -->
<Border Grid.Row="3" Padding="{Binding InnerContentPadding}">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
<Button Style="{StaticResource OkCancelButton}" Command="{Binding CloseCommand}" Content="Abbrechen"/>
<Button Style="{StaticResource OkCancelButton}" Command="{Binding AddressChangedCommand}" Content="Ok"/>
</StackPanel>
</Border>
负责的视图模型代码:
//Private Members
private bool mPropertiesChanged = false;
private string mFunctionSign;
private string mSpecs;
private string mSupplement;
//the canExecute-method
private bool SelectionChanged(object paramer)
{
return mPropertiesChanged;
}
//Public Properties
public string FunctionSign
{
get
{
return mFunctionSign;
}
set
{
mFunctionSign = value;
mPropertiesChanged = true;
OnPropertyChanged(nameof(FunctionSign));
}
}
public string Specs
{
get
{
return mSpecs;
}
set
{
mSpecs = value;
mPropertiesChanged = true;
OnPropertyChanged(nameof(Specs));
}
}
public string Supplement
{
get
{
return mSupplement;
}
set
{
mSupplement = value;
mPropertiesChanged = true;
OnPropertyChanged(nameof(Supplement));
}
}
当其中一个属性被更改并且相应的textbos失去焦点时,一切正常。
有没有办法让这个 mPropertiesChanged 为 true,而 textbos 仍然有焦点?
将绑定的UpdateSourceTrigger
设置为PropertyChanged
:
Text="{Binding FunctionSign, UpdateSourceTrigger=PropertyChanged}"
这将导致为每个击键设置源属性。默认值为LostFocus
。