Search code examples
c#.netmicrosoft-metrodependency-propertieswindows-store

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?


Solution

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