【问题标题】:Collapse CheckBox if ComboBox has no element selected如果 ComboBox 没有选择任何元素,则折叠复选框
【发布时间】:2022-01-19 18:11:32
【问题描述】:

如果ComboBox 的选定项目是null 或为空,我正在尝试折叠CheckBoxVisibility。源是具有两个字符串属性的对象列表:CodeName

我正在使用绑定到ComboBox 文本的触发器。

<ComboBox x:Name="VideoSub" SelectedItem="{Binding SubSelection, Mode=TwoWay}"
          ItemsSource="{Binding Path=SubsSource}"
          IsEnabled="{Binding HasItems, RelativeSource={RelativeSource Self}}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Path=Name}" />
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>
<CheckBox Width="80" IsEnabled="{Binding ElementName=VideoSub, Path=IsEnabled}"
          HorizontalAlignment="Right" Margin="0,10,0,0">
    <CheckBox.Style>
        <Style TargetType="{x:Type CheckBox}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Text.Length, ElementName=VideoSub, UpdateSourceTrigger=PropertyChanged}" Value="0">
                    <Setter Property="Visibility" Value="Collapsed"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </CheckBox.Style>
</CheckBox>

【问题讨论】:

    标签: c# wpf xaml data-binding


    【解决方案1】:

    更直接的是使用事件SelectionChanged来控制组件是否显示。

    我写了一些演示代码,希望对你有帮助:

    MainWindow.xaml

    <Window x:Class="TestWpfApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d" Title="MainWindow" Height="450" Width="800">        
        <StackPanel>
            <ComboBox Name="MyComboBox" SelectionChanged="ComboBox_SelectionChanged">
                <ComboBoxItem Content="Code"></ComboBoxItem>  
                <ComboBoxItem Content="Name"></ComboBoxItem>  
            </ComboBox>
            <CheckBox Name="MyCheckBox" Visibility="Collapsed" />
        </StackPanel>
    </Window>
    

    MainWindow.xaml.cs

    using System.Windows;
    using System.Windows.Controls;
    
    namespace TestWpfApp
    {
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                if (this.MyComboBox.SelectedItem == null)
                {
                    this.MyCheckBox.Visibility = Visibility.Collapsed;
                }
                else
                {
                    this.MyCheckBox.Visibility = Visibility.Visible;
                }
            }
        }
    }
    
    

    此外,有时在编程中使用过程(例如函数/方法)比定义(例如触发器等)更容易:)

    【讨论】:

      【解决方案2】:

      如果您使用DataTemplate,则不应触发其中的元素,而应触发其数据。如果您想在SelectedItemnull 时隐藏CheckBox,请使用x:Null 标记扩展进行比较。

      <DataTrigger Binding="{Binding SelectedItem, ElementName=VideoSub}" Value="{x:Null}">
         <Setter Property="Visibility" Value="Collapsed"/>
      </DataTrigger>
      

      当您将Name 绑定到TextBlock 时,您可以将DataTrigger 添加到您的样式中,将其与static empty string 进行比较。无需引用TextBlock 本身。

      <DataTrigger Binding="{Binding SelectedItem.Name, ElementName=VideoSub}" Value="{x:Static system:String.Empty}">
         <Setter Property="Visibility" Value="Collapsed"/>
      </DataTrigger>
      

      请注意,您必须为 String 类型添加 XML 命名空间。

      • .NET 核心:xmlns:system="clr-namespace:System;assembly=System.Runtime"
      • .NET 框架:xmlns:system="clr-namespace:System;assembly=mscorlib"

      这里是检查null 和空的完整样式。

      <Style TargetType="{x:Type CheckBox}">
         <Style.Triggers>
            <DataTrigger Binding="{Binding SelectedItem, ElementName=VideoSub}" Value="{x:Null}">
               <Setter Property="Visibility" Value="Collapsed" />
            </DataTrigger>
            <DataTrigger Binding="{Binding SelectedItem.(local:PersonModel.Name), ElementName=VideoSub}" Value="{x:Static system:String.Empty}">
               <Setter Property="Visibility" Value="Collapsed" />
            </DataTrigger>
         </Style.Triggers>
      </Style>
      

      顺便说一句,如果您只想在ComboBox 中将属性显示为文本,您可能根本不需要DataTemplate。改为设置DisplayMemberPath

      <ComboBox x:Name="VideoSub" SelectedItem="{Binding SubSelection, Mode=TwoWay}"
                ItemsSource="{Binding Path=SubsSource}"
                IsEnabled="{Binding HasItems, RelativeSource={RelativeSource Self}}"
                DisplayMemberPath="Name"/>
      

      【讨论】:

        【解决方案3】:

        只需绑定到SelectedItem:

        <CheckBox Width="80" IsEnabled="{Binding ElementName=VideoSub, Path=IsEnabled}"
                  HorizontalAlignment="Right" Margin="0,10,0,0">
            <CheckBox.Style>
                <Style TargetType="{x:Type CheckBox}">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding SelectedItem, ElementName=VideoSub}" Value="{x:Null}">
                            <Setter Property="Visibility" Value="Collapsed"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </CheckBox.Style>
        </CheckBox>
        

        “具有两个字符串属性:代码和名称”的对象本身不能为“空”。

        要么选择了一个项目,要么没有。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-01-17
          • 2021-12-16
          • 2016-08-22
          • 2011-06-26
          • 2014-07-11
          • 2021-01-25
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多