2017-01-04 5 views
1

私はカスタムコントロールアイコンを持っています。これには四角形が含まれています。私の設定親の値にデフォルトを設定します。しかし、時には私は任意の色に塗りつぶしを設定したいと思います。私はFillのDependencyPropertyを作成したくありません。代替はありますか? これは私のコードです:WPF - カスタムコントロールが長方形に塗りつぶされます

アイコンコントロール:

<UserControl> 
    <Rectangle Width="12" Height="12" 
       Fill="{Binding Path=Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Control}, AncestorLevel=3}}"> 
     <Rectangle.OpacityMask> 
      <VisualBrush Stretch="Fill" Visual="{Binding Path=Picture, RelativeSource={RelativeSource AncestorType={x:Type local:Icon}}}" /> 
     </Rectangle.OpacityMask> 
    </Rectangle> 
</UserControl> 



public partial class Icon 
    { 
     public static readonly DependencyProperty PictureProperty 
      = DependencyProperty.Register("Picture", typeof(Canvas), typeof(Icon)); 

     public Canvas Picture 
     { 
      get { return (Canvas)GetValue(PictureProperty); } 
      set { SetValue(PictureProperty, value); } 
     } 

     public Icon() 
     { 
      InitializeComponent(); 
     } 
    } 

使用 これは動作します:

<controls:Icon Picture="{StaticResource appbar_calendar}"/> 

これも希望:

<controls:Icon Picture="{StaticResource appbar_calendar}" Fill="Yellow"/> 

両方のオプションが欲しいです。 おかげ

+1

ControlTemplateでフォアグラウンド値を使用しているようですので、は期待通りに動作します。 – WPFUser

+1

ちょっとしたニックピッキング:カスタムコントロールと 'UserControl'は別のコンセプトです。あなたが 'UserControl'のための' ControlTemplate'を書いているのであれば、実際のカスタムコントロールを作るほうが良いでしょう。 – grek40

答えて

1

ユーザーコントロールを使用して、四角形を埋めるために、その背景プロパティを使用することができます何のFillプロパティを持っていないので:

Icon.xaml:

<UserControl x:Class="WpfApplication1.Icon" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:local="clr-namespace:WpfApplication1" 
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300" 
      x:Name="uc"> 
    <UserControl.Template> 
     <ControlTemplate TargetType="UserControl"> 
      <Rectangle Width="12" Height="12" 
        Fill="{TemplateBinding Background}"> 
       <Rectangle.OpacityMask> 
        <VisualBrush Stretch="Fill" Visual="{Binding Path=Picture, RelativeSource={RelativeSource AncestorType={x:Type local:Icon}}}" /> 
       </Rectangle.OpacityMask> 
      </Rectangle> 
     </ControlTemplate> 
    </UserControl.Template> 
</UserControl> 

Icon.xaml.cs:

public partial class Icon : UserControl 
{ 
    public static readonly DependencyProperty PictureProperty 
     = DependencyProperty.Register("Picture", typeof(Canvas), typeof(Icon)); 

    public Canvas Picture 
    { 
     get { return (Canvas)GetValue(PictureProperty); } 
     set { SetValue(PictureProperty, value); } 
    } 

    public Icon() 
    { 
     InitializeComponent(); 
    } 
} 

windows.xaml:

<controls:Icon Picture="{StaticResource appbar_calendar}" Background="Yellow"/> 

これは別の方法です。 Fillプロパティが本当に必要な場合は、もちろん、カスタムのIconクラスに「Fill」という別の依存関係プロパティを定義する必要があります。

関連する問題