2016-12-07 5 views
0

ButtonToggleButtonRadioButtonに幾何学プロパティを設定して、ControlTemplatesでこれらのコントロールにインスタンス固有のジオメトリを割り当てるときに定型句を使用しないようにします。このGeometryButtonクラスでDependencyPropertyをクラス階層のAttachedPropertyに置き換えることは可能ですか?

<my:GeometryButton Geometry="{StaticResource OneGeometry}"/> 
<my:GeometryButton Geometry="{StaticResource OtherGeometry}"/> 

<!-- ...and inside the Style for GeometryButton: --> 
<ContentControl TargetType="{x:Type my:GeometryButton}"> 
    <Border> 
     <Path Data={TemplateBinding Geometry}/> 
    </Border> 
</ContentControl> 

:たとえば

、現在私はこの書くことができます

public class GeometryButton : Button 
{ 
    static GeometryButton() 
    { 
     DefaultStyleKeyProperty.OverrideMetadata(typeof(GeometryButton), 
      new FrameworkPropertyMetadata(typeof(GeometryButton))); 
    } 


    public Geometry Geometry 
    { 
     get { return (Geometry)GetValue(GeometryProperty); } 
     set { SetValue(GeometryProperty, value); } 
    } 

    public static readonly DependencyProperty GeometryProperty = 
     DependencyProperty.Register("Geometry", 
            typeof(Geometry), 
            typeof(GeometryButton), 
            new PropertyMetadata(default(Geometry))); 

} 

を問題は、私はGeometryToggleButtonGeometryRadioButtonクラスを定義していた場合、私はになっています、ですDRYに違反する各クラスのDependencyPropertyコードを繰り返します。 また、RadioButtonはToggleButtonから派生し、ButtonとButtonはButtonBaseから派生しているので、私はこれを利用することができると思いますが、各クラスから別々に継承する必要がある場合は、継承のメリットはありません。

だから私はAttachedPropertiesを使用することが考えられるが、チュートリアルと例は、通常、いくつかの「親」の存在を示唆し、DockPanel.DockGrid.Left、またはControl.Foregroundのような例に言及、私はのわからない:

  • い最初に、AttachedPropertiesの概念が私のユースケースに当てはまりますか?
  • 「はい」の場合は、どうすれば実装できますか?

答えて

1

通常の添付プロパティを作成します。コントロールテンプレートで、それを使用します。

真剣に言うと、それ以上のことはありません。

たとえば、多くの異なるコントロールスタイルでCornerRadiusを指定できるように、添付のCornerRadiusプロパティを作成しました。このテンプレートはテンプレートで使用されます。

public static class Attached 
{ 
    public static readonly DependencyProperty CornerRadiusProperty 
      = DependencyProperty.RegisterAttached(
       "CornerRadius", 
       typeof(CornerRadius), 
       typeof(Attached), 
       new FrameworkPropertyMetadata(
        new CornerRadius(0), 
        FrameworkPropertyMetadataOptions.AffectsRender 
         | FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, 
        PropertyChanged) 
       ); 

    public static CornerRadius GetCornerRadius(DependencyObject uie) 
    { 
     return (CornerRadius)uie.GetValue(CornerRadiusProperty); 
    } 

    public static void SetCornerRadius(DependencyObject uie, CornerRadius value) 
    { 
     uie.SetValue(CornerRadiusProperty, value); 
    } 
} 

XAML

N.B.Bindingの中の(edcorpext:Attached.CornerRadius)の括弧は重要です。したがって、文字列は添付プロパティを参照する分割できない1つのパスセグメントです。それ以外の場合は、それをBinding.Sourceのプロパティへのパスとして解析し、:にヒットし、例外をスローします。

<ControlTemplate x:Key="EdCorpButtonTemplate" TargetType="{x:Type Button}"> 
    <Grid> 
     <Border 
      x:Name="PART_BackgroundBorder" 
      CornerRadius="{Binding (edcorpext:Attached.CornerRadius), RelativeSource={RelativeSource TemplatedParent}}" 
      BorderThickness="1.3" 
      BorderBrush="{StaticResource ControlBorderBrush}" 
      Background="{StaticResource EdCorpGrayMediumGradientBrush}" 
      SnapsToDevicePixels="True" 
      /> 
     <!-- etc. etc. etc. --> 

<Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}"> 
    <Setter Property="edcorpext:Attached.CornerRadius" Value="{StaticResource ButtonCornerRadius}" /> 
    <Setter Property="Template" Value="{StaticResource EdCorpButtonTemplate}" /> 

    <!-- etc. etc. etc. --> 

彼らは、私たちが「より現代的な」見て、このアプリケーションにUIを必要と教えてくれましたし、我々は彼がやっている知っている本物のデザイナーを持っていないので、私はものの上に非対称の角を丸く置きます。実際には以前よりずっと悪かったです。

+0

興味深い!あなたが提供した実装で、Buttonインスタンスに直接CornerRadiusを宣言することは可能でしょうか?例えば、

+0

@heltonbiker '

+0

さて、試して、フィードバックを投稿しよう!ありがとう! – heltonbiker

関連する問題