none
How to set the background with image when I use a style? RRS feed

  • Question

  • I am trying to se the background of a textbox but I need to use a style to can use some triggers. I have this code:

                                    <TextBox.Style>
                                        <Style TargetType="TextBox" BasedOn="{StaticResource TextBoxEncabezadoStyle}">
                                            <Setter Property="Visibility" Value="Visible"/>
                                            <Style.Triggers>
                                                <DataTrigger Binding="{Binding EmailContactoEmisor}" Value="{x:Null}">
                                                    <Setter Property="Visibility" Value="Hidden"/>
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding EmailContactoEmisor}" Value="">
                                                    <Setter Property="Visibility" Value="Hidden"/>
                                                </DataTrigger>
                                            </Style.Triggers>
                                        </Style>
                                    </TextBox.Style>

    I know that in this case I can set the background, but only a color, I am not able to set an image.

    If I use this code outside the style:

                                            <TextBox.Background>
                                                <ImageBrush Stretch="Uniform" ImageSource="/imagenes/email.png" AlignmentX="Left" AlignmentY="Center" />
                                            </TextBox.Background>

    But then the image is not shown, if I comment the style I can see the image. So I guess that the style has preference over the textBox.Background.

    In sumary, I would like to can use the style to can use the triggers and also I would like to use and image as background.

    Thanks.

    Friday, August 16, 2019 9:03 PM

Answers

  • Hi,
    you can use resource to solve your problem like in this demo:

    <Window.Resources>
        <ImageBrush x:Key="Img" Stretch="Uniform" ImageSource="Image.jpg" AlignmentX="Left" AlignmentY="Center" />
      </Window.Resources>
      <StackPanel>
        <TextBox Text="{Binding EmailContactoEmisor, UpdateSourceTrigger=PropertyChanged}"/>
        <TextBox>
          <TextBox.Style>
            <Style TargetType="TextBox">
              <Setter Property="Background" Value="{x:Null}"/>
              <Style.Triggers>
                <DataTrigger Binding="{Binding EmailContactoEmisor}" Value="{x:Null}">
                  <Setter Property="Background" Value="{StaticResource Img}"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding EmailContactoEmisor}" Value="">
                  <Setter Property="Background" Value="{StaticResource Img}"/>
                </DataTrigger>
              </Style.Triggers>
            </Style>
          </TextBox.Style>
        </TextBox>
      </StackPanel>


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    • Marked as answer by ComptonAlvaro Tuesday, August 27, 2019 10:19 AM
    Sunday, August 18, 2019 5:51 AM