WPF MultiDataTrigger AND 条件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31982354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF MultiDataTrigger AND condition
提问by Steven
I would like to enable a button only when both of my two datagrids have selected items. Right now it is enabled when either of the datagrids have selections. Any ideas?
我想仅当我的两个数据网格都选择了项目时才启用按钮。现在,当其中一个数据网格有选择时,它就会启用。有任何想法吗?
<Button x:Name="button" Content="Z" Grid.Column="1" Margin="0,240,0,0" VerticalAlignment="Top" FontFamily="Wingdings 3" FontSize="21.333" ToolTip="Set the selected alarm for the selected alarm time">
<Button.Style>
<Style TargetType="Button">
<Setter Property="IsEnabled" Value="True" />
<Setter Property="Opacity" Value="1" />
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding ElementName=alarmProfilesDataGrid, Path=SelectedItem}" Value="{x:Null}"/>
<Condition Binding="{Binding ElementName=alarmFilesDataGrid, Path=SelectedItem}" Value="{x:Null}"/>
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter Property="IsEnabled" Value="False" />
<Setter Property="Opacity" Value=".5" />
</MultiDataTrigger.Setters>
</MultiDataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
回答by Yogesh
Here is what happens with your code: Instead of both conditions to be true when either of the datagrids have selections, the conditions are only met when both of the datagrid don't have selections.
以下是您的代码发生的情况:当两个数据网格中的任何一个都有选择时,这两个条件都不是,只有当两个数据网格都没有选择时才满足条件。
When the program starts, both the datagrids are nulls, so your condition is met. Now if you make selection in either of your grids, your condition is never met and the value of IsEnabledremains Truei.e. the original value.
程序启动时,两个数据网格都是空值,因此满足您的条件。现在,如果您在任一网格中进行选择,则永远不会满足您的条件,并且 的值IsEnabled仍然True是原始值。
To correct this problem, you need a converter:
要解决此问题,您需要一个转换器:
public class NotNullToBoolConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
bool result = value == null ? false : true;
return result;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return value;
}
}
and
和
<Style TargetType="Button">
<Setter Property="IsEnabled" Value="False" />
<Setter Property="Opacity" Value=".5" />
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding ElementName=alarmProfilesDataGrid, Path=SelectedItem, Converter={StaticResource NotNullToBoolConverter}}" Value="True"/>
<Condition Binding="{Binding ElementName=alarmFilesDataGrid, Path=SelectedItem, Converter={StaticResource NotNullToBoolConverter}}" Value="True"/>
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter Property="IsEnabled" Value="True" />
<Setter Property="Opacity" Value="1" />
</MultiDataTrigger.Setters>
</MultiDataTrigger>
</Style.Triggers>
</Style>
回答by mkb
I believe the right way to do this is using Property bindings and a Command with CanExecute conditions which is bound to the Button.
我相信正确的方法是使用属性绑定和绑定到按钮的具有 CanExecute 条件的命令。
public RelayCommand SaveCommand { get; set; }
SaveCommand = new RelayCommand();
SaveCommand.Action = () => { Save(SelectedObject1,SelectedObject2); };
SaveCommand.CanExecute = () => { SelectedObject1 != null && SelectedObject2 != null) };
XAML:
XAML:
<DataGrid SelectedItem="{Binding Path=SelectedObject1}">
</DataGrid>
<DataGrid SelectedItem="{Binding Path=SelectedObject2}">
</DataGrid>
<Button Context="Save" Command="{Binding SaveCommand}/>
This will enable the button only when two object is selected from each grid.
这将仅在从每个网格中选择两个对象时启用该按钮。
PS: This code is not valid but you can find how to use a RelayCommand on SO. Please search for some RelayCommand implementation on SO.
PS:此代码无效,但您可以找到如何在 SO 上使用 RelayCommand。请在 SO 上搜索一些 RelayCommand 实现。

