0
votes

I have a UserControl which is so defined

<UserControl x:Class="Controls.wTextBox"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:Controls"
         xmlns:sys="clr-namespace:System;assembly=mscorlib"
         mc:Ignorable="d" 
         d:DesignHeight="20" d:DesignWidth="150" Height="20" Width="150" BorderThickness="1" BorderBrush="LightGray" x:Name="PART_UserControl" Background="White">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>
    <TextBox Grid.Column="0" HorizontalAlignment="Stretch" VerticalAlignment="Center" BorderBrush="Transparent" BorderThickness="0" Name="PART_TextBox">
        <TextBox.Style>
            <Style TargetType="TextBox">
                <Style.Triggers>
                    <Trigger Property="Text" Value="{x:Static sys:String.Empty}">
                        <Setter Property="Background">
                            <Setter.Value>
                                <VisualBrush AlignmentX="Left" AlignmentY="Center" Stretch="None">
                                    <VisualBrush.Visual>
                                        <Label Foreground="DarkGray" Content="????"/>
                                    </VisualBrush.Visual>
                                </VisualBrush>
                            </Setter.Value>
                        </Setter>
                    </Trigger>
                    <Trigger Property="Text" Value="{x:Null}">
                        <Setter Property="Background">
                            <Setter.Value>
                                <VisualBrush AlignmentX="Left" AlignmentY="Center" Stretch="None">
                                    <VisualBrush.Visual>
                                        <Label Foreground="DarkGray" Content="{Binding HintText, ElementName=PART_UserControl}"/>
                                    </VisualBrush.Visual>
                                </VisualBrush>
                            </Setter.Value>
                        </Setter>
                    </Trigger>
                    <Trigger Property="IsKeyboardFocused" Value="True">
                        <Setter Property="Background" Value="White" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </TextBox.Style>
    </TextBox>
    <Button Grid.Column="1" Click="OnButtonClick" Width="18" BorderBrush="Transparent" BorderThickness="0" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center" Height="18" Focusable="False">
        <Button.Content>
            <Path Stretch="Fill" Fill="#000000" Margin="2" Data="M29.898 26.5722l-4.3921 0c-0.0118,-0.635 -0.0177,-1.0172 -0.0177,-1.1583 0,-1.4229 0.2352,-2.5929 0.7056,-3.5102 0.4704,-0.9231 1.417,-1.952 2.8281,-3.1044 1.4111,-1.1465 2.2578,-1.8991 2.5282,-2.2578 0.4292,-0.5585 0.6409,-1.1818 0.6409,-1.8579 0,-0.9408 -0.3763,-1.7463 -1.1289,-2.4224 -0.7526,-0.6703 -1.7639,-1.0054 -3.0397,-1.0054 -1.2289,0 -2.2578,0.3527 -3.0868,1.0524 -0.8232,0.6997 -1.3935,1.7698 -1.7051,3.2044l-4.4391 -0.5527c0.1234,-2.0578 0.9995,-3.8041 2.6223,-5.2387 1.6286,-1.4346 3.757,-2.152 6.4029,-2.152 2.7752,0 4.9859,0.7291 6.6322,2.1814 1.6404,1.4522 2.4635,3.1397 2.4635,5.0741 0,1.0642 -0.3057,2.0755 -0.9054,3.028 -0.6056,0.9525 -1.8933,2.2519 -3.8688,3.8923 -1.0231,0.8525 -1.6581,1.5346 -1.905,2.052 -0.2469,0.5174 -0.3587,1.4405 -0.3351,2.7752zm-4.3921 6.5087l0 -4.8389 4.8389 0 0 4.8389 -4.8389 0z"/>
        </Button.Content>
    </Button>
</Grid>

using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;

namespace Controls
{
/// <summary>
/// Interaktionslogik für wTextBox.xaml
/// </summary>
public partial class wTextBox : UserControl, INotifyPropertyChanged
{
    /// <summary>
    /// Event, das ausgelöst wird, wenn sich eine Property ändert.
    /// </summary>
    public event PropertyChangedEventHandler PropertyChanged;

    #region "DependencyProperties"
    /// <summary>
    /// Bindungsproperty für den Wert, der in der TextBox angezeigt wird
    /// </summary>
    public static DependencyProperty ValueProperty;
    public static DependencyProperty DisplayTextProperty;
    public static DependencyProperty HintTextProperty;
    public static DependencyProperty HintTextColorProperty;
    public static DependencyProperty ShowHintTextProperty;
    #endregion

    /// <summary>
    /// Wert der TextBox
    /// </summary>
    public object Value
    {
        get { return (object)this.GetValue(wTextBox.ValueProperty); }
        set { this.SetValue(wTextBox.ValueProperty, value); this.OnPropertyChanged("Value"); this.OnPropertyChanged("DisplayText"); this.OnPropertyChanged("HintText"); }
    }

    public string DisplayText
    {
        get { return (Value == null) ? "" : Value.ToString(); }
        set { this.SetValue(wTextBox.ValueProperty, value); this.OnPropertyChanged("Value"); this.OnPropertyChanged("DisplayText"); this.OnPropertyChanged("HintText"); }
    }

    public string HintText
    {
        get { return (string)this.GetValue(wTextBox.HintTextProperty); }
        set { this.SetValue(wTextBox.HintTextProperty, value); this.OnPropertyChanged("HintText"); }
    }

    public Brush HintTextColor
    {
        get { return (Brush)this.GetValue(wTextBox.HintTextColorProperty); }
        set { this.SetValue(wTextBox.HintTextColorProperty, value); this.OnPropertyChanged("HintTextColor"); }
    }

    static wTextBox()
    {
        ValueProperty = DependencyProperty.RegisterAttached("Value", typeof(object), typeof(wTextBox), new PropertyMetadata(""));
        DisplayTextProperty = DependencyProperty.RegisterAttached("DisplayText", typeof(object), typeof(wTextBox), new PropertyMetadata(""));
        HintTextProperty = DependencyProperty.RegisterAttached("HintText", typeof(string), typeof(wTextBox), new PropertyMetadata(""));
        HintTextColorProperty = DependencyProperty.RegisterAttached("HintTextColor", typeof(Brush), typeof(wTextBox), new PropertyMetadata(new SolidColorBrush(Colors.LightGray)));
    }

    public wTextBox()
    {
        InitializeComponent();

        this.PART_TextBox.DataContext = this;

        this.MouseEnter += OnMouseEnterHover;
        this.MouseLeave += OnMouseLeaveHover;
    }


    protected void SetBorderColor(bool IsHoverColor)
    {
        if (IsHoverColor)
        {
            this.BorderBrush = new SolidColorBrush(Colors.DarkBlue);
        }
        else
        {
            if(!this.PART_TextBox.IsFocused)
                this.BorderBrush = new SolidColorBrush(Colors.LightGray);
        }
    }


    private void OnMouseLeaveHover(object sender, MouseEventArgs e)
    {
        wTextBox tbObjekt = sender as wTextBox;
        if (tbObjekt != null)
        {
            tbObjekt.SetBorderColor(false);
        }
    }

    private void OnMouseEnterHover(object sender, MouseEventArgs e)
    {
        wTextBox tbObjekt = sender as wTextBox;
        if (tbObjekt != null)
        {
            tbObjekt.SetBorderColor(true);
        }
    }


    /// <summary>
    /// Methode, von jedem Setter einer Property aufgerufen werden muss
    /// </summary>
    /// <param name="propertyName">Name der Property</param>
    public void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }



    /// <summary>
    /// Methode die ausgeführt wird, wenn der Button gedrückt wird
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void OnButtonClick(object sender, RoutedEventArgs e)
    {

    }

}
}

In the wTextBox Class is a DependencyProperty HintText to which I want to bind the content of the Label. Can someone tell me how I have to define the binding?

I tried it with
Content="{Binding ElementName=PART_UserControl, Path=HintText}" and
Content="{Binding HintText, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:wTextBox}}}"

Both do not seem to work

1
According to this post from @AnatoliyNikolaev, it can't be done that way -- but the post does suggest workarounds.Petter Hesselberg
Thank you for the link. It's a pity that it is not possible that way. So i will try it with the workaround.Marcel W.

1 Answers

1
votes

Ok, for everybody interested, here is my solution. After a time of experementing i found out, that it is working, when the brush is defined as Resource of the UserControl

<UserControl x:Class="Controls.wTextBox"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:Controls"
         xmlns:sys="clr-namespace:System;assembly=mscorlib"
         mc:Ignorable="d" 
         d:DesignHeight="20" d:DesignWidth="150" Height="20" Width="150" BorderThickness="1" BorderBrush="LightGray" x:Name="PART_UserControl" Background="White">
<UserControl.Resources>
    <VisualBrush x:Key="HintTextBrush" AlignmentX="Left" AlignmentY="Center" Stretch="None">
        <VisualBrush.Visual>
            <Label Foreground="DarkGray" Content="{Binding ElementName=PART_UserControl, Path=HintText}"/>
        </VisualBrush.Visual>
    </VisualBrush>
    <SolidColorBrush x:Key="ClearHinTextBrush" Color="White"/>
</UserControl.Resources>
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>
    <TextBox Grid.Column="0" HorizontalAlignment="Stretch" VerticalAlignment="Center" BorderBrush="Transparent" BorderThickness="0" Name="PART_TextBox">
        <TextBox.Style>
            <Style TargetType="TextBox">
                <Style.Triggers>
                    <Trigger Property="Text" Value="{x:Static sys:String.Empty}">
                        <Setter Property="Background" Value="{Binding Source={StaticResource HintTextBrush}}"/>
                    </Trigger>
                    <Trigger Property="Text" Value="{x:Null}">
                        <Setter Property="Background" Value="{Binding Source={StaticResource HintTextBrush}}"/>
                    </Trigger>
                    <Trigger Property="IsKeyboardFocused" Value="True">
                        <Setter Property="Background" Value="{Binding Source={StaticResource ClearHinTextBrush}}"/>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </TextBox.Style>
    </TextBox>
    <Button Grid.Column="1" Click="OnButtonClick" Width="18" BorderBrush="Transparent" BorderThickness="0" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center" Height="18" Focusable="False">
        <Button.Content>
            <Path Stretch="Fill" Fill="#000000" Margin="2" Data="M29.898 26.5722l-4.3921 0c-0.0118,-0.635 -0.0177,-1.0172 -0.0177,-1.1583 0,-1.4229 0.2352,-2.5929 0.7056,-3.5102 0.4704,-0.9231 1.417,-1.952 2.8281,-3.1044 1.4111,-1.1465 2.2578,-1.8991 2.5282,-2.2578 0.4292,-0.5585 0.6409,-1.1818 0.6409,-1.8579 0,-0.9408 -0.3763,-1.7463 -1.1289,-2.4224 -0.7526,-0.6703 -1.7639,-1.0054 -3.0397,-1.0054 -1.2289,0 -2.2578,0.3527 -3.0868,1.0524 -0.8232,0.6997 -1.3935,1.7698 -1.7051,3.2044l-4.4391 -0.5527c0.1234,-2.0578 0.9995,-3.8041 2.6223,-5.2387 1.6286,-1.4346 3.757,-2.152 6.4029,-2.152 2.7752,0 4.9859,0.7291 6.6322,2.1814 1.6404,1.4522 2.4635,3.1397 2.4635,5.0741 0,1.0642 -0.3057,2.0755 -0.9054,3.028 -0.6056,0.9525 -1.8933,2.2519 -3.8688,3.8923 -1.0231,0.8525 -1.6581,1.5346 -1.905,2.052 -0.2469,0.5174 -0.3587,1.4405 -0.3351,2.7752zm-4.3921 6.5087l0 -4.8389 4.8389 0 0 4.8389 -4.8389 0z"/>
        </Button.Content>
    </Button>
</Grid>

So no workaround is needed.