[英]MultiDataTrigger with multiple ComboBoxes WPF
I would like to enable button only when all specified ComboBoxes
have values.我只想在所有指定的ComboBoxes
都有值时启用按钮。 However, as soon as I add a second condition, button is enabled from the start但是,只要我添加第二个条件,按钮就会从一开始就启用
Here is my code这是我的代码
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding ElementName=SensorPartNumberComboBox, Path=SelectedValue}" Value="{x:Null}"/>
<Condition Binding="{Binding ElementName=SensorTypeComboBox, Path=SelectedValue}" Value="{x:Null}"/>
<Condition Binding="{Binding ElementName=SensorBrandNameComboBox, Path=SelectedValue}" Value="{x:Null}"/>
<Condition Binding="{Binding ElementName=DimmingProtocolComboBox, Path=SelectedValue}" Value="{x:Null}"/>
<Condition Binding="{Binding ElementName=Wired_WirelessComboBox, Path=SelectedValue}" Value="{x:Null}"/>
</MultiDataTrigger.Conditions>
<Setter Property="IsEnabled" Value="False"/>
</MultiDataTrigger>
</Style.Triggers>
I would appreciate any help!我将不胜感激任何帮助!
MultiDataTrigger
requires all of the conditions to be true for it to take effect. MultiDataTrigger
需要所有条件都为真才能生效。 Ie it's equivalent to a logical AND.即它相当于一个逻辑与。
In your example, if any value is non-null, the trigger won't take effect and the button will remain enabled.在您的示例中,如果任何值不为空,则触发器将不会生效,并且按钮将保持启用状态。
For a logical OR, instead of using MultiDataTrigger
, just use multiple DataTrigger
.对于逻辑 OR,而不是使用MultiDataTrigger
,只需使用多个DataTrigger
。 If any condition of any trigger is true, then that trigger will take effect, with precedence over the default setter for the property in the style.如果任何触发器的任何条件为真,则该触发器将生效,优先于样式中属性的默认设置器。
For example:例如:
<Setter Property="IsEnabled" Value="True"/>
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=SensorPartNumberComboBox, Path=SelectedValue}" Value="{x:Null}">
<Setter Property="IsEnabled" Value="False"/>
<DataTrigger/>
<DataTrigger Binding="{Binding ElementName=SensorTypeComboBox, Path=SelectedValue}" Value="{x:Null}">
<Setter Property="IsEnabled" Value="False"/>
<DataTrigger/>
<DataTrigger Binding="{Binding ElementName=SensorBrandNameComboBox, Path=SelectedValue}" Value="{x:Null}">
<Setter Property="IsEnabled" Value="False"/>
<DataTrigger/>
<DataTrigger Binding="{Binding ElementName=DimmingProtocolComboBox, Path=SelectedValue}" Value="{x:Null}">
<Setter Property="IsEnabled" Value="False"/>
<DataTrigger/>
<DataTrigger Binding="{Binding ElementName=Wired_WirelessComboBox, Path=SelectedValue}" Value="{x:Null}">
<Setter Property="IsEnabled" Value="False"/>
<DataTrigger/>
</Style.Triggers>
Alternatively, you might consider putting the logic in your view model, with a single bool
property you bind to, and which is set according to the bound SelectedValue
properties for the various ComboBox
controls.或者,您可以考虑将逻辑放在您的视图 model 中,使用您绑定到的单个bool
属性,并根据各种ComboBox
控件的绑定SelectedValue
属性进行设置。
Yet another alternative would be to use MultiBinding
to bind the five view model properties that are bound to the ComboBox.SelectedValue
properties, with a IMultiValueConverter
that implements the logic.另一种选择是使用MultiBinding
将绑定到ComboBox.SelectedValue
属性的五个视图 model 属性与实现逻辑的IMultiValueConverter
绑定。
Of course, these last two options work only if you've got a proper view model with binding set up in the first place (something I strongly encourage, if you haven't already).当然,这最后两个选项只有在您拥有正确的视图 model 并首先设置绑定时才有效(如果您还没有的话,我强烈建议您这样做)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.