基于SelectedItem设置ComboBox的IsEnabled属性

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)

  • 我认为Converter的实现是错误的,它应该是<return value!= null>并且不需要声明实例.至少我没有一个,它的工作原理. (4认同)
  • @KrisTrip您需要在某处实际声明Converter的实例.注意@ColinE将它引用为`StaticResource`.这意味着你需要一个包含你的ComboBox的资源集合中的一个条目,如下所示:`<NullToFalseConverterClass x:Key = NullToFalseConverter />` (3认同)

Pav*_*kov 5

是的,您可以将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)