DataTrigger使WPF按钮处于非活动状态,直到文本框具有价值
我要的按钮
控件的属性是的IsEnabled =FALSE
直至值输入到一个文本框
在窗口
。
I want the Button
control's property to be IsEnabled="False"
until a value is entered into a TextBox
in the Window
.
code到目前为止:
<Button
Content="Click Me"
Name="ClickMe"
VerticalAlignment="Top"
Click="ClickMe_Click">
<Button.Style>
<Style>
<Style.Triggers>
<DataTrigger
Binding="{Binding ElementName=textBox, Path=Length}"
<!-- or even: Binding="{Binding Path=textBox.Length}" -->
Value="0">
<Setter
Property="Button.IsEnabled"
Value="false" />
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
此外,是有可能有这种按钮
控件的的IsEnabled
属性是基于3种不同的文本框
控制所有具有价值?
Also, is it possible to have this Button
control's IsEnabled
property be based on 3 different TextBox
controls all having values?
假设你正在采用presentation模式,比如一个视图模型,你应该直接绑定的UI元素的数据,而不是。
Assuming you are employing a presentation model, such as a ViewModel, you should bind directly the data instead of the UI elements.
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding FirstName}" Value="{x:Null}" />
<Condition Binding="{Binding MiddleName}" Value="{x:Null}" />
<Condition Binding="{Binding LastName}" Value="{x:Null}" />
</MultiDataTrigger.Conditions>
<Setter Property="Button.IsEnabled" Value="False" />
</MultiDataTrigger>
</Style.Triggers>
这是说,如果你使用的是presentation模式,你可以随时添加一个布尔
EnableSave财产和处理所有的presentation逻辑有而不是在视图本身。
That said, if you are using a presentation model, you can always add a bool
"EnableSave" property and handle all the presentation logic there instead of in the view itself.
由于可以通过下面的评论中可以看出,我错误地设置这使按钮
时的任意的文本框
具有价值,但要求是按钮
启用时的所有的文本框
ES具有价值。
As can be seen by following the comments, I mistakenly set this up to enable the Button
when any TextBox
has a value, but the requirement is that the Button
be enabled when all TextBox
es have values.
从概念上讲,所有你需要做的是反向的条件 - 而不是假,如果所有条件假的,我们想要的。如此,如果所有条件属实
Conceptually, all you have to do is reverse the conditions -- instead of "false if all conditions false," we want "true if all conditions true."
美中不足的是,有没有办法在XAML说不空 - 没办法不使用的IValueConverter
,那是。我想创建一个 NullToBoolConverter
的回报假
为空
和真
为!= NULL
。
The catch is that there is no way to say "not null" in the XAML -- no way without an IValueConverter
, that is. I'd create a NullToBoolConverter
that returns false
for null
and true
for != null
.
由于这种转换器:
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding FirstName,
Converter={StaticResource NullToBoolConverter}}" Value="True" />
<Condition Binding="{Binding MiddleName,
Converter={StaticResource NullToBoolConverter}}" Value="True" />
<Condition Binding="{Binding LastName,
Converter={StaticResource NullToBoolConverter}}" Value="True" />
</MultiDataTrigger.Conditions>
<Setter Property="Button.IsEnabled" Value="True" />
</MultiDataTrigger>
</Style.Triggers>