Home > Software engineering >  trigger textbox collapse on combobox set to index 0
trigger textbox collapse on combobox set to index 0

Time:12-15

I am making my own WPF user control, to give the user options to select data. I have a Combobox that has a style that is located in a separate resource dictionary. I want to collapse the Textbox if the Combobox's SelectedIndex is set to 0.

Here is my code :

         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>`

I use Microsoft blend to make my triggers however it is not giving me an option to set up a Property base trigger in my new user control.

CodePudding user response:

It is more directly to use event SelectionChanged to control whether components are displayed.

I wrote some demo code, hope it helps:

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;
            }
        }
    }
}
  • Related