【问题标题】:Radio buttons - showing two diff list with data in the view WPF单选按钮 - 在视图 WPF 中显示两个不同的数据列表
【发布时间】:2019-01-20 23:01:04
【问题描述】:

我在视图中有一些代码:

<StackPanel Orientation="Horizontal" Background="{StaticResource GrayBrush8}">
        <RadioButton Content="Radio1" />
        <RadioButton Content="Radio2" />         
    </StackPanel>
    <ct:DynamicFilterPanel Grid.Row="1" 
                           ItemsType="{x:Type db:RadioData1}"
                           DbContext="{Binding DataContext}"
                           FilterQuery="{Binding FilterQuery, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                           ChildCollectionsFilters="{Binding SubTypesFilter}"
                           WindowType="{x:Type mah:MetroWindow}"
                           />
    <ct:BDataGrid  Grid.Row="2" IsReadOnly="True" ItemsSource="{Binding Model.List}" SelectedItem="{Binding Model.SelectedItem}" />

我有一些包含列、模型和视图模型的数据和名称的类。 当我启动应用程序时,它会显示带有 RadioData1 数据的列。好的。第一个单选按钮就是为此。但是,当我单击第二个单选按钮时,我该怎么做,它会显示另一个包含 RadioData2 列的列表。什么样的转换器?或者我必须在此视图中进行更改。

【问题讨论】:

    标签: c# wpf radio-button


    【解决方案1】:

    您必须编辑 XAML

    <StackPanel Orientation="Horizontal" Background="{StaticResource GrayBrush8}">
                <RadioButton Name="Radio1" Content="Radio1" DataContextChanged="RadioButton_DataContextChanged" />
                <RadioButton Name="Radio2" Content="Radio2" />
            </StackPanel>
    

    在 CS 文件中

    添加方法

    private void RadioButton_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
            {
                if (Radio1.IsChecked==true)
                {
                    //your Code
                }
                else
                {
                    //your other code
                }
            }
    

    你可以使用可见性在 XAML 中显示或隐藏某些东西

    <Button Name="btn1" Visibility="Hidden" />
    

    并像这样在您的 CS 文件中使用

    if (Radio1.IsChecked==true)
                {
                                   btn1.Visibility = Visibility.Visible;
                }
    

    【讨论】:

    • 另一个问题:我可以通过单选按钮更改视图模型 因为这取决于
    • 是的,我认为您可以使用 并且在单选按钮中可以将 False 更改为 True
    猜你喜欢
    • 2021-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-30
    • 2013-05-13
    • 2014-06-19
    相关资源
    最近更新 更多