将DependencyProperty绑定到usercontrol

时间:2012-01-12 13:08:08

标签: c# wpf xaml binding dependency-properties

我创建了一个依赖属性但由于某种原因它似乎失败了,它必须与绑定有关但我看不清楚。依赖属性如下:

public class HighlightColour
{
    #region HighlightColour dependency property

    public static readonly DependencyProperty sm_ValueProperty;

    public static Color GetValue(DependencyObject obj)
    {
        return (Color)obj.GetValue(sm_ValueProperty);
    }

    public static void SetValue(DependencyObject obj, Color value)
    {
        obj.SetValue(sm_ValueProperty, value);
    }

    #endregion

    static HighlightColour()
    {
        var metadata = new FrameworkPropertyMetadata(Colors.Transparent);
        sm_ValueProperty = DependencyProperty.RegisterAttached("Value",
                                                   typeof(Color),
                                                   typeof(HighlightColour), metadata);
    }
}

在XAML中我试图在样式模板中绑定它并设置按钮的背景颜色:

<Style x:Key="CommonButton" TargetType="Button">
    <Setter Property="FontFamily" Value="Tahoma"/>
    <Setter Property="FontSize" Value="12"/>
    <Setter Property="custprop:HighlightColour.Value" Value="Red"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Grid Margin="0">
                    <Border Name="ButtonBody" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1" CornerRadius="12,12,12,12" />
                    <Border Name="Highlight" Opacity="0" BorderThickness="0" CornerRadius="12,12,12,12">
                        <Border.Background>
                            <SolidColorBrush Color="{Binding Path=(custprop:HighlightColour.Value), Source={RelativeSource FindAncestor,AncestorType={x:Type Button}}}"/>
                        </Border.Background>
                    </Border>
                    <ContentPresenter Name="content" HorizontalAlignment="Center" VerticalAlignment="Center" />
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Trigger.EnterActions>
                            <BeginStoryboard Name="HighlightAnim">
                                <Storyboard TargetName="Highlight" TargetProperty="Opacity" AutoReverse="True" RepeatBehavior="Forever">
                                    <DoubleAnimation From="1" To="0.2" Duration="00:00:01"/>
                                </Storyboard>
                            </BeginStoryboard>
                        </Trigger.EnterActions>
                        <Trigger.ExitActions>
                            <StopStoryboard BeginStoryboardName="HighlightAnim"/>
                        </Trigger.ExitActions>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

由于某种原因,它失败了,我看到了错误,例如:

  

无法从''获取'价值'值(输入'颜色')(类型   '的RelativeSource')。 BindingExpression:路径=(0);   DataItem ='RelativeSource'(HashCode = 60828848);目标元素是   'SolidColorBrush'(HashCode = 10588721);目标属性是'颜色'   (输入'Color')InvalidCastException:'System.InvalidCastException:   无法将“System.Windows.Data.RelativeSource”类型的对象强制转换为   输入'System.Windows.DependencyObject'。

2 个答案:

答案 0 :(得分:2)

变化:

    <SolidColorBrush Color="{Binding Path=(custprop:HighlightColour.Value), 
                     Source={RelativeSource FindAncestor,AncestorType={x:Type Button}}}" />

要:

<SolidColorBrush Color="{Binding Path=(custprop:HighlightColour.Value), 
                 RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Button}}}" />

有关绑定的详细信息:MSDNthis cheat sheet

答案 1 :(得分:0)

请勿使用sm_作为前缀,否则WPF将无法“找到”您的DP。