开发者

Bind to a TemplatedParent's name in WPF?

I'm trying to create a WPF Custom Control which will bind itself to it's templated parent's x:Name property. The code which I believe should do this which was also generated by the Binding Maker in Visual Studio, is as follows:

Binding RelativeSource={RelativeSource TemplatedParent}, Path=Name, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay

But this produces no result. I can replace the binding with any plain text and the desired behavior (the text overlays itself on the control when and only when the user has typed nothing into the control), but my goal here is to make this work as a sort of tooltip so the user knows what the field is supposed to be (as defined in the field's x:Name property).

Here's my full xaml:

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SuperTB">
<Style TargetType="{x:Type local:SuperTextB}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:SuperTextB}">
                <Border Background="{TemplateBinding Background}"
                    BorderBrush="{TemplateBinding BorderBrush}"
                    BorderThickness="{TemplateBinding BorderThickness}">
                    <TextBox Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Text, Mode=TwoWay, UpdateSourceTrigger=LostFocus}">
                        <TextBox.Style>
                            <Style TargetType="TextBox">
                                <Style.Triggers>
                                    <Trigger Property="Text" Value="">
                                        <Setter Property="Background">
                                            <Setter.Value>
                                                <VisualBrush Stretch="None">
                                                    <VisualBrush.Visual>
                                                        <TextBlock Foreground="Gray" FontSize="24" Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsRequired}"></TextBlock>
                                                    </VisualBrush.Visual>
                                                </VisualBrush>
                                            </Setter.Value>
                                        </Setter>
                                    </Trigger>
                      开发者_C百科              <DataTrigger Binding="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsRequired}" Value="True">
                                        <Setter Property="BorderThickness" Value="4" />
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </TextBox.Style>
                    </TextBox>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

And the control's c#:

    public class SuperTextB : Control
{

    static SuperTextB()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(SuperTextB), new FrameworkPropertyMetadata(typeof(SuperTextB)));

    }

    public static DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(String), typeof(SuperTextB));

    public string Text
    {
        get { return (String)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }


    private static DependencyProperty myNameProperty =
DependencyProperty.Register("MyName", typeof(string), typeof(SuperTextB), new PropertyMetadata("Unicorns !", NameChanged));

    private static void NameChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {

    }

    public string MyName
    {
        get { return (string)GetValue(myNameProperty); }
        set { SetValue(myNameProperty, value); }
    }
    DependencyProperty isRequiredProperty =
        DependencyProperty.Register("IsRequired", typeof(bool), typeof(SuperTextB), new PropertyMetadata(false, IsReqChanged));

    private static void IsReqChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {

    }

    public bool IsRequired
    {
        get { return (bool)GetValue(isRequiredProperty); }
        set { SetValue(isRequiredProperty, value); }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜