0

我为 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}"/>

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

4

2 回答 2

1

改变你的FramesCount定义:

public int FramesCount
{
    get { return (string)GetValue(FramesCountProperty ); }
    set 
    { 
        SetValue(FramesCountProperty , value); 
        if (ImageFileMask != null) ReloadFrames();
    }
}
于 2013-06-29T16:52:05.727 回答
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;
            })
        );
于 2013-07-02T12:29:57.307 回答