[英]Collapse CheckBox if ComboBox has no element selected
如果null
ComboBox
為空,我正在嘗試通過Visibility
折疊CheckBox
。 源是具有兩個字符串屬性的對象列表: Code
和Name
。
我正在使用綁定到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>
更直接的是使用事件SelectionChanged
來控制是否顯示組件。
我寫了一些演示代碼,希望對您有所幫助:
主窗口.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>
主窗口.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;
}
}
}
}
此外,有時在編程中使用過程(例如函數/方法)比定義(例如觸發器等)更容易:)
如果您使用的是DataTemplate
,則不應觸發其中的元素,而應觸發其數據。 如果要在SelectedItem
為null
時隱藏CheckBox
,請使用x:Null
標記擴展進行比較。
<DataTrigger Binding="{Binding SelectedItem, ElementName=VideoSub}" Value="{x:Null}">
<Setter Property="Visibility" Value="Collapsed"/>
</DataTrigger>
當您將Name
綁定到TextBlock
時,您可以將DataTrigger
添加到您的樣式中,將其與static 空字符串進行比較。 無需引用TextBlock
本身。
<DataTrigger Binding="{Binding SelectedItem.Name, ElementName=VideoSub}" Value="{x:Static system:String.Empty}">
<Setter Property="Visibility" Value="Collapsed"/>
</DataTrigger>
請注意,您必須為String
類型添加 XML 命名空間。
xmlns:system="clr-namespace:System;assembly=System.Runtime"
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"/>
只需綁定到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>
object 本身“具有兩個字符串屬性:代碼和名稱”不能為“空”。
要么選擇了一個項目,要么沒有。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.