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

问题描述 投票:2回答:1

我在同一wpf窗口中有2个组合框。第一个组合框中的第一项是selectversion,默认情况下处于选中状态(isselected = true)。现在,如果选择了第一个组合框的第一项,则我需要禁用第二个组合框,否则启用。

我尝试关注,

If(absversion.selectedindex !=0) 

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

Else
  Secondcombo.isenabled = true:

page_loaded事件中,

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

任何人都可以帮助我,以完成此工作。

wpf wpf-controls
1个回答
3
投票

我更倾向于在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>
© www.soinside.com 2019 - 2024. All rights reserved.