0

I created a style for a textbox in order to set up a watermark for a search box. It's working well but now , the TextChanged event I used to filter out a Listview Content is not triggered. Here's the xaml from my page

   <TextBox
                Style="{StaticResource WatermarkTextBox}"
                Name="txtFilter"
                DockPanel.Dock="left"
                TextChanged="txtFilter_TextChanged"  />

And the Style :

    <Style x:Key="WatermarkTextBox" TargetType="{x:Type TextBox}">
    <Setter Property="BorderBrush" Value="Black" />
    <Setter Property="BorderThickness" Value="1" />
    <!--<EventSetter Event="TextChanged" Handler="{Binding TextChanged}" />-->
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TextBox}">
                <Border
                    Background="{TemplateBinding Background}"
                    BorderBrush="{TemplateBinding BorderBrush}"
                    BorderThickness="{TemplateBinding BorderThickness}">
                    <Grid>
                        <TextBlock
                            HorizontalAlignment="Center"
                            VerticalAlignment="Center"
                            Opacity="0.6"
                            Text="Search...">
                            <TextBlock.Visibility>
                                <MultiBinding Converter="{StaticResource WaterMarkBoolToVisibilityConverter}">
                                    <Binding ElementName="inputText" Path="Text.IsEmpty" />
                                    <Binding ElementName="inputText" Path="IsFocused" />
                                </MultiBinding>
                            </TextBlock.Visibility>
                        </TextBlock>
                        <TextBox x:Name="inputText" Background="Transparent" />
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>

How to keep my txtFilter_TextChanged event from my main page ? It seems I can define a EventSetter in the style but I cannot get the same behavior than for the property, binding it back to the original property value from the control ( Background="{TemplateBinding Background}" for example)

SadE
  • 1
  • 1
  • Is that `UserControl`? You may subsribe in MainWindow to `UserControl`'s event in code-behind `txtFilter.TextChanged += txtFilter_TextChanged`. Or learn something about [MVVM](https://learn.microsoft.com/en-us/archive/msdn-magazine/2009/february/patterns-wpf-apps-with-the-model-view-viewmodel-design-pattern), `DataContext` and `Binding`. WPF is powerful but not when you're using it like Winforms. – aepot May 19 '20 at 22:07
  • Ops, not sorry for wrong suggestion above. The event is not firing because it fired in templated `TextBox`. Not in the parent one. Set the event handler in embedded `TextBox` which has name `x:Name="inputText"`. – aepot May 19 '20 at 22:10
  • A style like this will not work, you're basically destroying the normal `TextBox` `ControlTemplate`. You'd be better off just putting a `TextBlock` on top of a normal `TextBox` in a `Grid`. Or using any of the answers from this question: https://stackoverflow.com/questions/833943/watermark-hint-text-placeholder-textbox – Keith Stein May 20 '20 at 03:18
  • Does this answer your question? [Watermark / hint text / placeholder TextBox](https://stackoverflow.com/questions/833943/watermark-hint-text-placeholder-textbox) – Keith Stein May 20 '20 at 03:19

0 Answers0