2013-06-28 178 views
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