简体   繁体   English

如何根据单选按钮控制 ComboBox 的可见性被选中

[英]How to control Visibility of ComboBox based on radio button is checked

I have a quick WPF question in regards to the visibility of my ComboBox with respects to whether or not a button is checked or not.我有一个快速的 WPF 问题,关于我的ComboBox在是否选中按钮方面的可见性。 The goal is that when the user checks the radio button: 'btnCurrent', the ComboBox : cboHistorySequence will be hidden, and when the button 'btnHistory' is checked, it will appear.我们的目标是,当用户选中的单选按钮:“btnCurrent”,该ComboBoxcboHistorySequence将被隐藏,而当按钮“btnHistory”被选中,它就会出现。

VIEW: Here we have the radio button 'btnCurrent' and 'btnHistory', as well as combobox cboHistorySequence.视图:这里有单选按钮“btnCurrent”和“btnHistory”,以及组合框 cboHistorySequence。

 <RadioButton x:Name="btnCurrent" IsChecked="{Binding IsCurrentSelected, UpdateSourceTrigger=PropertyChanged}" Content="Current" Grid.Column="0" Grid.Row="0"/>

 <RadioButton x:Name="btnHistory" IsChecked="{Binding IsHistorySelected, UpdateSourceTrigger=PropertyChanged}" Content="History" Grid.Row="0" Grid.Column="1"/>

 <StackPanel Orientation="Horizontal" Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2" Margin="5,0" IsEnabled="{Binding IsHistorySelected, Converter={StaticResource EnabledConverter} }">
     <TextBlock  HorizontalAlignment="Left" Width="80">History Seq:</TextBlock>
     <ComboBox x:Name="cboHistorySequence" Margin="16,0,0,0" Text="{Binding Path=HistorySequence, UpdateSourceTrigger=PropertyChanged}" Width="80" HorizontalAlignment="Left">
         <ComboBoxItem>First</ComboBoxItem>
         <ComboBoxItem>Last</ComboBoxItem>
     </ComboBox>
 </StackPanel>

What I have tried我试过的

My initial thought was to use something along the lines of this and bind it over to the view-model, but I have not been successful.我最初的想法是使用类似的东西并将其绑定到视图模型,但我没有成功。 What are yall's recommendations? yall 有什么建议?

Visibility="{Binding IsShowComboBox, Converter={StaticResource VisibilityConverter}

Because you want to bind to the property of another element in your application you should use Binding.ElementName Property and Path , something like this:因为您想绑定到应用程序中另一个元素的属性,您应该使用Binding.ElementName属性和Path ,如下所示:

<ComboBoxItem>Last</ComboBoxItem>
<ComboBox.Style>
    <Style TargetType="{x:Type ComboBox}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding  ElementName=btnCurrent, Path=IsChecked}" Value="True">
                <Setter Property="Visibility" Value="Hidden" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</ComboBox.Style>

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

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