user1291867
user1291867

Reputation: 131

ImageBrush Opacity Binding to a TextBox

I would like to show and hide an image as background of a TextBox depending on the TextBox's content. For this purpose I used an ImageBrush and regulate the opacity with a binding to the TextBox's Text property with a value converter:

<TextBox Height="23" 
     HorizontalAlignment="Left" 
     Margin="175,47,0,0" 
     VerticalAlignment="Top" 
     Width="120">
<TextBox.Style>
    <Style TargetType="{x:Type TextBox}">
        <Style.Resources>

            <!-- Converter -->
            <local:EmptyStringToNotOpacityConverter x:Key="EmptyStringToNotOpacityConverter" />

        </Style.Resources>
        <Setter Property="Background">
            <Setter.Value>
                <ImageBrush ImageSource="search.png" 
                            Stretch="None" 
                            AlignmentX="Right" 
                            AlignmentY="Center"
                             Opacity="{Binding RelativeSource={RelativeSource AncestorType=TextBox}, 
                                              Path=Text, 
                                              Converter={StaticResource EmptyStringToNotOpacityConverter}, Mode=OneWay}"
                            />
            </Setter.Value>
        </Setter>
    </Style>
</TextBox.Style>

The value converter ist pretty straight forward and converts an empty string to 1d and anything else to 0d, thus showing the image if the TextBox is empty and hiding the image if the TextBox is not empty.

Everything works like expected but I am not able to get rid of a binding error during startup:

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.TextBox', AncestorLevel='1''. BindingExpression:Path=Text; DataItem=null; target element is 'ImageBrush' (HashCode=41973697); target property is 'Opacity' (type 'Double')

Has anybody come across this behaviour and found a workaround to prevent the binding error?

Upvotes: 0

Views: 971

Answers (1)

user1291867
user1291867

Reputation: 131

After consultation with Microsoft and one support case later it came out that this is still an unfixed Bug in Visual Studio 2010 (Bug Dev10 | 817794) which has no known consequences for the executable.

However, as I am confronted with lots of these error messages in VS2010 the important error messages get lost in the output window. Thus, I tried to find a workaround and came up with a solution using a VisualBrush instead of an ImageBrush:

<Window x:Class="CSVisualBrush.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Visual Brush Opacity Binding" Height="350" Width="525">
    <Grid>
        <TextBox Name="txt" 
                 Height="23" 
                 HorizontalAlignment="Left" 
                 Margin="175,47,0,0" 
                 VerticalAlignment="Top" 
                 Width="120">
            <TextBox.Style>
                <Style TargetType="{x:Type TextBox}">
                    <Setter Property="Background">
                        <Setter.Value>
                            <VisualBrush Stretch="None" 
                                         AlignmentX="Right"
                                         AlignmentY="Center"
                                         >
                                <VisualBrush.Visual>
                                    <Image Name="img" Source="/CSVisualBrush;component/search.png" />
                                </VisualBrush.Visual>
                            </VisualBrush>
                        </Setter.Value>
                    </Setter>
                </Style>
            </TextBox.Style>
        </TextBox>
    </Grid>
</Window>

As the Binding doesn't work when defined in the XAML, I create it in the code behind:

public MainWindow() {
    InitializeComponent();

    // Set binding to opacity of the image
    // REMARK: Binding doesn't work within XAML
    Image img = (Image)((VisualBrush)txt.Background).Visual;
    Binding b = new Binding();
    b.Source = txt;
    b.Path = new PropertyPath("Text");
    b.Converter = new EmptyStringToNotOpacityConverter();
    img.SetBinding(Image.OpacityProperty, b);
}

Up to know, I don't know reason why the binding works in code but not in XAML. If anbody runs in the same problems I put the workaround VS2010 solution on my website www.logiclink.de.

Upvotes: 1

Related Questions