簡體   English   中英

如何根據單選按鈕控制 ComboBox 的可見性被選中

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

我有一個快速的 WPF 問題,關於我的ComboBox在是否選中按鈕方面的可見性。 我們的目標是,當用戶選中的單選按鈕:“btnCurrent”,該ComboBoxcboHistorySequence將被隱藏,而當按鈕“btnHistory”被選中,它就會出現。

視圖:這里有單選按鈕“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>

我試過的

我最初的想法是使用類似的東西並將其綁定到視圖模型,但我沒有成功。 yall 有什么建議?

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

因為您想綁定到應用程序中另一個元素的屬性,您應該使用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