Kri*_*rip 6 c# wpf combobox datatrigger isenabled
我想根据另一个ComboBox中是否选择了一个项来启用/禁用ComboBox.我能够通过在Style上设置触发器来使其工作,但这会覆盖我对组合框的自定义全局样式.有没有另一种方法来获得相同的功能而不会失去我的风格?
<ComboBox Grid.Column="1" Grid.Row="1"
Name="AnalysisComboBox"
MinWidth="200"
VerticalAlignment="Center" HorizontalAlignment="Left"
ItemsSource="{Binding Path=AvailableAnalysis}">
<ComboBox.Style>
<Style TargetType="{x:Type ComboBox}">
<Setter Property="IsEnabled" Value="True" />
<Style.Triggers>
<DataTrigger Binding="{Binding SelectedItem,ElementName=ApplicationComboBox}" Value="{x:Null}">
<Setter Property="IsEnabled" Value="False" />
</DataTrigger>
</Style.Triggers>
</Style>
</ComboBox.Style>
</ComboBox>
Run Code Online (Sandbox Code Playgroud)
Col*_*inE 10
您不需要通过Style执行此操作,您可以使用值转换器直接绑定IsEnabled属性,如下所示:
<ComboBox Grid.Column="1" Grid.Row="1"
Name="AnalysisComboBox"
MinWidth="200"
VerticalAlignment="Center" HorizontalAlignment="Left"
IsEnabled={Binding SelectedItem, ElementName=ApplicationComboBox, Converter={StaticResource NullToFalseConverter}}"
ItemsSource="{Binding Path=AvailableAnalysis}"/>
Run Code Online (Sandbox Code Playgroud)
其中NullToFalseConverter是以下转换器实例的键:
public class NullToFalseConverter: IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value == null;
}
public object ConvertBack(object value, Type targetType,
object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
Run Code Online (Sandbox Code Playgroud)
是的,您可以将BasedOn属性设置为"继承"您的全局样式:
<ComboBox Grid.Column="1" Grid.Row="1"
Name="AnalysisComboBox"
MinWidth="200"
VerticalAlignment="Center" HorizontalAlignment="Left"
ItemsSource="{Binding Path=AvailableAnalysis}">
<ComboBox.Style>
<Style TargetType="{x:Type ComboBox}"
BasedOn="{StaticResource {x:Type ComboBox}}">
<Setter Property="IsEnabled" Value="True" />
<Style.Triggers>
<DataTrigger Binding="{Binding SelectedItem,ElementName=ApplicationComboBox}" Value="{x:Null}">
<Setter Property="IsEnabled" Value="False" />
</DataTrigger>
</Style.Triggers>
</Style>
</ComboBox.Style>
</ComboBox>
Run Code Online (Sandbox Code Playgroud)
而不是{StaticResource {x:Type ComboBox}}你可以设置你的全局样式的键(如果它不是隐式的).
但是对于这个特定任务,您不需要定义样式.您可以设置绑定到IsEnabled属性并使用转换器将另一个组合框的选定项转换为布尔值:
<ComboBox Grid.Column="1" Grid.Row="1"
Name="AnalysisComboBox"
MinWidth="200"
VerticalAlignment="Center" HorizontalAlignment="Left"
ItemsSource="{Binding Path=AvailableAnalysis}"
IsEnabled="{Binding SelectedItem,ElementName=ApplicationComboBox, Converter={StaticResource NotNullConverter}"/>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11066 次 |
| 最近记录: |