如何通过选择组合框项目使无线电按钮可见?

时间:2016-08-11 11:43:32

标签: wpf xaml

我有一个带3个项目和两个单选按钮的组合框。如果我选择item1,则radioButton1应该可见,第二个应该被隐藏。如果选择了item2,则该过程应该反之亦然,通过选择item3,两个单选按钮都应该可见。

2 个答案:

答案 0 :(得分:0)

如果使用c#进行编码,可以在comboBoxSelectedIndexChanged事件函数中使用switch-case分支。对于可见性,应使用Visible类,您可以使用布尔值控制。 在C#中有类似的东西(未经测试):

void ComboBox1SelectedIndexChanged(object sender, EventArgs e)
{
        Object selectedItem = comboBox1.SelectedItem;
        switch (selectedItem.ToString())
        {
            case "item1":
                radioButton1.Visible = true;
                radioButton2.Visible = false;
                break;
            case "item2":
                radioButton1.Visible = false;
                radioButton2.Visible = true;
                break;
            case "item3":
                radioButton1.Visible = true;
                radioButton2.Visible = true;
                break;
            default:
                radioButton1.Visible = false;
                radioButton2.Visible = false;
                break;
        }
}

答案 1 :(得分:0)

试试这个:

<Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>

        <ComboBox Grid.Row="0" x:Name="combo">
            <ComboBoxItem x:Name="item1" Content="item1" IsSelected="True"></ComboBoxItem>
            <ComboBoxItem x:Name="item2" Content="item2"></ComboBoxItem>
            <ComboBoxItem x:Name="item3" Content="item3"></ComboBoxItem>
        </ComboBox>
        <StackPanel Grid.Row="1">
            <RadioButton Content="1">
                <RadioButton.Style>
                    <Style>
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding IsSelected, ElementName=item1}" Value="True">
                                <Setter Property="RadioButton.Visibility" Value="Visible"></Setter>
                            </DataTrigger>
                            <DataTrigger Binding="{Binding IsSelected, ElementName=item1}" Value="False">
                                <Setter Property="RadioButton.Visibility" Value="Hidden"></Setter>
                            </DataTrigger>
                            <DataTrigger Binding="{Binding IsSelected, ElementName=item3}" Value="True">
                                <Setter Property="RadioButton.Visibility" Value="Visible"></Setter>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </RadioButton.Style>
            </RadioButton>
            <RadioButton Content="2">
                <RadioButton.Style>
                    <Style>
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding IsSelected, ElementName=item2}" Value="True">
                                <Setter Property="RadioButton.Visibility" Value="Visible"></Setter>
                            </DataTrigger>
                            <DataTrigger Binding="{Binding IsSelected, ElementName=item2}" Value="False">
                                <Setter Property="RadioButton.Visibility" Value="Hidden"></Setter>
                            </DataTrigger>
                            <DataTrigger Binding="{Binding IsSelected, ElementName=item3}" Value="True">
                                <Setter Property="RadioButton.Visibility" Value="Visible"></Setter>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </RadioButton.Style>
            </RadioButton>
        </StackPanel>
    </Grid>