6

I'm new to WPF and I 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>
1
  • To Which property your selected value of the comboBox is Binded..??
    – Vishal
    Jul 18, 2013 at 11:31

3 Answers 3

7

That Trigger won't work because you have set Visibility property explicitly in TextBox

Do it like 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>

The reason for this is Dependency property value precedence.

6

There is a

<BooleanToVisibilityConverter x:Key="BoolToVis"></BooleanToVisibilityConverter>

You can use it as following

<TextBox Visibility="{Binding YourPropertyName, Converter={StaticResource BoolToVis}}"></TextBox>
0

If I got your question right:-

If your selected value is binded to some property in the ViewModel like:-

private string _GenderType;

        public string GenderType
        {
            get
            {
                return _GenderType;
            }
            set
            {
                _GenderType= value;
 RaisePropertyChanged("GenderType");

In xaml:-

<TextBox.Style>
        <Style>
<Setter Property="TextBox.Visibility" value="Hidden"/>
<Style TargetType="{x:Type TextBox}">   
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=GenderType,ElementName=Combo1}" Value="Other">
                      <Setter Property="Visibility" Value="Visible"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBox.Style>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.