0

我为城市应用程序制作了自定义控件,并且希望从样式中设置其属性。但是它并没有被调用。从样式XAML设置自定义控件属性

Control属性:

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德,崔根源财产FramesCount才不是。它的setter只有在我直接在页面XAML中设置而不是设置样式时才会调用。 有人知道我在做什么错吗?

回答

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

更改FramesCount定义:

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

我试图做到这一点,但没有任何反应...... – Serhii