WPF Showing / Hiding a control with triggers

Total Post:104

Points:728
WPF 
WPF
 2649  View(s)
Ratings:
Rate this:
Hi experts!

I am trying to create xaml logic to show / hide a control based on the value of the AllowMiscTitle on the ViewModel. The xaml consist of two fields a combobox of the standard tiles ("Mr", "Mrs", ..., "Other") when "Other" is selected I want the textbox to display.

I've created the follow xaml:

                <DockPanel Validation.Error="Validation_Error" HorizontalAlignment="Stretch">

                <ComboBox ItemsSource="{Binding Path=Titles, Mode=OneTime}"

                      Text="{Binding Path=Title}"/>

                <TextBox x:Name="TxtBxTitle" Margin="5,5" Visibility="Visible">

                    <TextBox.Style>

                        <Style>

                            <Style.Triggers>

                                <DataTrigger Binding="{Binding Path=AllowMiscTitle}" Value="false">

                                    <Setter Property="TextBox.Visibility" Value="Hidden"/>

                                </DataTrigger>

                            </Style.Triggers>

                        </Style>

                    </TextBox.Style>

                </TextBox>

            </DockPanel>

thanks in advance

  1. Post:105

    Points:735
    Re: WPF Showing / Hiding a control with triggers

    Solution!

    That trigger won't work because you set Visibility explicitly in TextBox

    try this..

    <TextBox x:Name="TxtBxTitle"
    Margin="5,5">
                    <TextBox.Style>
                        <Style>
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding Path=AllowMiscTitle}" Value="false">
                                    <Setter Property="TextBox.Visibility" Value="Hidden"/>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </TextBox.Style>
                </TextBox>

      Modified On Mar-30-2018 05:13:51 AM

Answer