【问题标题】:Setting custom control properties from Style XAML从样式 XAML 设置自定义控件属性
【发布时间】:2013-06-26 09:22:36
【问题描述】:

我为 Metro 应用程序制作了自定义控件,并希望通过 Style 设置其属性。但它的 setter 没有被调用。

控件属性:

    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 中设置它而不是设置样式时,它的设置器才会调用。 有人知道我做错了什么吗?

【问题讨论】:

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


    【解决方案1】:

    更改您的 FramesCount 定义:

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

    【讨论】:

      【解决方案2】:

      我找到了一些解决方案:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-07-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多