简体   繁体   English

如何禁用基于 wpf 中其他组合框的组合框

[英]How to disable combobox based on other combobox in wpf

I have 2 combobox in a same wpf window.我在同一个 wpf 窗口中有 2 个组合框。 The first item in first combobox is selectversion and it is selected by default(isselected=true).第一个组合框中的第一项是 selectversion,默认情况下它是选中的(isselected=true)。 Now i need the second combobox to be disabled if the first item of first combobox is selected Otherwise enabled.现在我需要禁用第二个组合框,如果第一个组合框的第一个项目被选中否则启用。

I tried following,我试着跟随,

If(absversion.selectedindex !=0) 

  Secondcombo.isenabled=false://here i am getting null reference exception 

Else
  Secondcombo.isenabled = true:

In page_loaded event I have,page_loaded事件中,我有,

Secondcombo.isenabled = false //so that Secondcombo will be disabled by default wen loaded. 

Could anyone please help me,to get this done.任何人都可以帮助我完成这项工作。

I'd be more inclined to do this in XAML than in codebehind:与代码隐藏相比,我更倾向于在 XAML 中执行此操作:

<Grid x:Name="LayoutRoot">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <ComboBox Grid.Row="0" x:Name="ComboBox1">
        <ComboBoxItem>Item 1</ComboBoxItem>
        <ComboBoxItem>Item 2</ComboBoxItem>
        <ComboBoxItem>Item 3</ComboBoxItem>
    </ComboBox>
    <ComboBox Grid.Row="1">
        <ComboBox.Style>
            <Style TargetType="{x:Type ComboBox}">
                <Setter Property="IsEnabled" Value="True" />
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ElementName=ComboBox1, Path=SelectedIndex}" Value="0">
                        <Setter Property="IsEnabled" Value="False" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </ComboBox.Style>
        <ComboBoxItem>Item 1</ComboBoxItem>
        <ComboBoxItem>Item 2</ComboBoxItem>
        <ComboBoxItem>Item 3</ComboBoxItem>
    </ComboBox>
</Grid>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM