Akiner Alkan
Akiner Alkan

Reputation: 6918

How to write trigger to textbox IskeyboardFocused property

I am trying to change other UI element visibility when textbox is focused. I think that isKeyboardFocused event may solve my problem. So i write my xaml as following.

                <TextBox x:Name="SearchBox" Text="{Binding SearchText, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Stretch">
                    <i:Interaction.Triggers>
                        <e:PropertyChangedTrigger>
                            <i:Interaction.Behaviors>
                                <e:ConditionBehavior>
                                    <e:ConditionalExpression>
                                        <e:ComparisonCondition LeftOperand="{Binding SearchBox.IsKeyboardFocused}" Operator="Equal" RightOperand="rue" />
                                    </e:ConditionalExpression>
                                </e:ConditionBehavior>
                            </i:Interaction.Behaviors>
                            <e:ChangePropertyAction TargetName="SearchLabel" PropertyName="Visibility" Value="Hidden" />
                        </e:PropertyChangedTrigger>
                    </i:Interaction.Triggers>
                </TextBox>
                <TextBlock x:Name="SearchLabel" Text="Search" FontStyle="Italic" Foreground="Gray" IsHitTestVisible="False" Visibility="Visible" />

This trigger is not working and i could'nt find the problem. What can i do to fix this problem or any other idea about it?

Upvotes: 1

Views: 1261

Answers (2)

Akiner Alkan
Akiner Alkan

Reputation: 6918

Well, I also find another answer which solves my problem easily. It may be also useful who has the same problem like me.

<TextBox>
<TextBox.Style>
    <Style TargetType="TextBox" xmlns:sys="clr-namespace:System;assembly=mscorlib">
        <Style.Resources>
            <VisualBrush x:Key="CueBannerBrush" AlignmentX="Left" AlignmentY="Center" Stretch="None">
                <VisualBrush.Visual>
                    <Label Content="Search" Foreground="LightGray" />
                </VisualBrush.Visual>
            </VisualBrush>
        </Style.Resources>
        <Style.Triggers>
            <Trigger Property="Text" Value="{x:Static sys:String.Empty}">
                <Setter Property="Background" Value="{StaticResource CueBannerBrush}" />
            </Trigger>
            <Trigger Property="Text" Value="{x:Null}">
                <Setter Property="Background" Value="{StaticResource CueBannerBrush}" />
            </Trigger>
            <Trigger Property="IsKeyboardFocused" Value="True">
                <Setter Property="Background" Value="White" />
            </Trigger>
        </Style.Triggers>
    </Style>
</TextBox.Style>
</TextBox>

Upvotes: 1

WPFGermany
WPFGermany

Reputation: 1647

I would bind the Visibility to the IsKeyboardFocused property as follows:

<UserControl.Resources>
    <YourNamespace:BoolToVisConverter x:Key="BoolToVis" />
</UserControl.Resources>
...
<TextBox x:Name="SearchBox" />
<Label Visibility="{Binding ElementName=SearchBox, Path=IsKeyboardFocused, Converter={StaticResource BoolToVis}, ConverterParameter='invert'}">hello</Label>

using a customized BoolToVisibilityConverter:

public class BoolToVisConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (!(value is bool)) throw new ArgumentException("bool value expected");

        Visibility invisibleMode = (parameter == null || !(parameter is string) ||
                                    !((string) parameter).ToLower().Contains("hidden"))
                                       ? Visibility.Collapsed
                                       : Visibility.Hidden;

        if ((parameter as string)?.ToLower().Contains("invert") ?? false) return (!(bool) value) ? Visibility.Visible : invisibleMode;

        return ((bool) value) ? Visibility.Visible : invisibleMode;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

The Converter parses given CommandParameter string for: - "invert": true --> invisible, false --> visible - "hidden": invisble case leads to Visibilitys.Hidden, otherwise it's Visibility.Collapsed

Upvotes: 2

Related Questions