简体   繁体   中英

Setting custom control properties from Style XAML

I made custom control for metro application and want to set its properties from Style. But it's setters are not called.

Control property:

    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:

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

And page XAML:

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

Standard properties (Width and Height) are setted properly, byt costom property FramesCount does not. Its setter calls only when I set it directly in page XAML instead setting style. Does anybody know what I'm doing wrong?

Change your FramesCount definition:

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

I found some solution:

    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;
            })
        );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM