WPF-自定义控件集填充到矩形

WPF-自定义控件集填充到矩形,wpf,Wpf,我有自定义控件图标,其中包含来自矩形的。我设置填充默认值为父值。但有时我想将填充设置为任何颜色。我不想为Fill创建DependencyProperty。替代方案是什么? 这是我的代码: 图标控件: <UserControl> <Rectangle Width="12" Height="12" Fill="{Binding Path=Foreground, RelativeSource={RelativeSource FindAncest

我有自定义控件图标,其中包含来自矩形的。我设置填充默认值为父值。但有时我想将填充设置为任何颜色。我不想为Fill创建DependencyProperty。替代方案是什么? 这是我的代码:

图标控件:

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

公共部分类图标
{
公共静态只读从属属性PictureProperty
=DependencyProperty.Register(“图片”、类型(画布)、类型(图标));
公共画布图片
{
获取{return(Canvas)GetValue(PictureProperty);}
set{SetValue(PictureProperty,value);}
}
公共图标()
{
初始化组件();
}
}
用法 这项工作:

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

这还需要:

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

我想要两种选择。
谢谢

由于UserControl没有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>
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();
    }
}
<controls:Icon Picture="{StaticResource appbar_calendar}" Background="Yellow"/>
Window.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>
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();
    }
}
<controls:Icon Picture="{StaticResource appbar_calendar}" Background="Yellow"/>


这是另一种选择。如果您真的想要填充属性,您当然必须在自定义图标类中定义另一个名为“填充”的依赖属性。

您似乎在ControlTemplate中使用前景值,因此将按预期工作。只是一些吹毛求疵:自定义控件和
UserControl
是不同的概念。如果您发现自己正在为
UserControl
编写
ControlTemplate
,那么最好创建一个实际的自定义控件。