从Style XAML设置自定义控件属性

时间:2013-06-28 14:46:29

标签: c# .net microsoft-metro dependency-properties windows-store

我为metro应用程序进行了自定义控制,并希望从Style设置其属性。但它的定制者不会被召唤。

控制属性:

    public int FramesCount
    {
        get { return _framesCount; }
        set
        {
            _framesCount = value;
            if (ImageFileMask != null) ReloadFrames();
        }
    }

    public static readonly DependencyProperty FramesCountProperty =
        DependencyProperty.Register(
            "FramesCount", typeof(int),
            typeof(MyControl), null
        );

XAML风格:

<Style TargetType="controls:MyControl" x:Key="wmLoadingBoxWaiting">
    <Setter Property="Width" Value="32"/>
    <Setter Property="Height" Value="32"/>
    <Setter Property="FramesCount" Value="1"/>
</Style>

和页面XAML:

<controls:MyControl HorizontalAlignment="Left" Margin="645,185,0,0" VerticalAlignment="Top" Style="{StaticResource wmLoadingBoxWaiting}"/>

标准属性(宽度和高度)设置正确,byt costom属性FramesCount没有。只有当我直接在页面XAML中设置它而不是设置样式时,它才会调用它。 有人知道我做错了吗?

2 个答案:

答案 0 :(得分:1)

更改FramesCount定义:

public int FramesCount
{
    get { return (string)GetValue(FramesCountProperty ); }
    set 
    { 
        SetValue(FramesCountProperty , value); 
        if (ImageFileMask != null) ReloadFrames();
    }
}

答案 1 :(得分:0)

我找到了一些解决方案:

    public int FramesCount
    {
        get { return _framesCount; }
        set
        {
            _framesCount = value;
            if (ImageFileMask != null) ReloadFrames();
        }
    }

    public static readonly DependencyProperty FramesCountProperty =
        DependencyProperty.Register(
            "FramesCount",
            typeof(int),
            typeof(MyControl),
            new PropertyMetadata(false, (d, e) =>
            {
                (d as MyControl).FramesCount = (int)e.NewValue;
            })
        );