在设置为索引 0 的组合框上触发文本框折叠

trigger textbox collapse on combobox set to index 0

我正在制作自己的 WPF 用户控件,以便为用户提供 select 数据的选项。我有一个 Combobox,其样式位于单独的资源字典中。如果组合框的 SelectedIndex 设置为 0,我想折叠文本框。

这是我的代码:

         UserControl x:Class="Baileys.CustomChartControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:Baileys"
         mc:Ignorable="d" 
         d:DesignHeight="81.855" Loaded ="UserControl_Loaded" MouseDoubleClick="UserControl_DoubleClick"  MouseDown="UserControl_MouseDown"  Width="759.405" >
<Grid x:Name="grid" Background="Transparent" Margin="0,0,-368,-23">     `
<ComboBox HorizontalAlignment="Left" Height="100" Margin="173,99,0,-123"VerticalAlignment="Top" Style="{DynamicResource CBstyle}" Width="120"/>
  <TextBlock x:Name="MyCoursesTxt" Text="{Binding MyCourses}"  />
  </Grid>`

我使用 Microsoft blend 来制作我的触发器,但是它没有给我在我的新用户控件中设置 属性 基本触发器的选项。

使用事件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 x:Name="MyComboBox" SelectionChanged="ComboBox_SelectionChanged">
            <ComboBoxItem Content="Test1"></ComboBoxItem>  
            <ComboBoxItem Content="Test2"></ComboBoxItem>  
        </ComboBox>
        <TextBlock x:Name="MyCoursesTxt" Text="This is TextBlock." />
    </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.SelectedIndex == 0)
            {
                this.MyCoursesTxt.Visibility = Visibility.Collapsed;
            }
            else
            {
                this.MyCoursesTxt.Visibility = Visibility.Visible;
            }
        }
    }
}