WPF绑定ControlTemplate

时间:2018-04-10 17:31:22

标签: c# .net wpf xaml

我有一个非常讨厌的问题。

我在自定义控件中声明了两个依赖项属性。

    /// Property for the color of the border
    public static readonly DependencyProperty LineColorProperty = DependencyProperty.Register("LineColor", typeof(Brush), typeof(CCInputBox), new PropertyMetadata(Brushes.Black));
    public Brush LineColor
    {
        get { return (Brush)GetValue(LineColorProperty); }
        set { SetValue(LineColorProperty, value); }
    }

这一个

    /// Property for the color if the ui element is focused
    /// </summary>
    public static readonly DependencyProperty FocusColorProperty = DependencyProperty.Register("FocusColor", typeof(Brush), typeof(CCInputBox), new PropertyMetadata(Brushes.Gray));
    public Brush FocusColor
    {
        get { return (Brush)GetValue(FocusColorProperty); }
        set { SetValue(FocusColorProperty, value); }
    }

现在我想在ControlTemplate触发器中将LineColor绑定到FocusColor,以便在UIElement聚焦时显示新的LineColor。

    <ControlTemplate.Triggers>
        <Trigger Property="Text" Value="">
            <Setter Property="Visibility" TargetName="plcHolder" Value="Visible" />
        </Trigger>
        <Trigger Property="IsFocused" Value="True">
            <Setter Property="Visibility" TargetName="plcHolder" Value="Collapsed" />
            <Setter Property="LineColor" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=FocusColor}" />
        </Trigger>
    </ControlTemplate.Triggers>

触发器工作正常,只是将lineColor属性设置为他自己的默认值,即黑色,而不是FocusColor值。 这里也是我用于UIElement的样式,我设置了FocusColor。

  <Style TargetType="{x:Type CCControls:CCInputBox}">
    <Setter Property="FontFamily" Value="Calibri" />
    <Setter Property="FontSize" Value="15" />
    <Setter Property="FocusVisualStyle" Value="{x:Null}" />
    <Setter Property="LineColor" Value="LightGray" />
    <Setter Property="FocusColor" Value="Green" />
    <Setter Property="Cursor" Value="IBeam" />
    <Setter Property="TextWrapping" Value="NoWrap" />
    <Setter Property="PlaceholderColor" Value="{StaticResource CCGrayBrush}" />
    <Setter Property="Template" Value="{StaticResource CCInputBoxTemplate}" />
</Style>

有没有人知道如何解决这个问题?

提前感谢。

0 个答案:

没有答案