将依赖项属性附加到用户控件

本文关键字:用户 控件 依赖 属性 | 更新日期: 2023-09-27 18:30:42

如何在Rectangle填充时注册DependencyProperty,以便我可以更改颜色动态?

<UserControl.Resources>
    <Style x:Key="ButtonStyle1" TargetType="{x:Type Button}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid>
                        <Rectangle Stroke="Black">
                            <Rectangle.Fill>
                                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                    <GradientStop Color="#FF48B6E4" Offset="0.013"/>
                                    <GradientStop Color="#FF091D8D" Offset="1"/>
                                </LinearGradientBrush>
                            </Rectangle.Fill>
                        </Rectangle>
                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsFocused" Value="True"/>
                        <Trigger Property="IsDefaulted" Value="True"/>
                        <Trigger Property="IsMouseOver" Value="True"/>
                        <Trigger Property="IsPressed" Value="True"/>
                        <Trigger Property="IsEnabled" Value="False"/>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</UserControl.Resources>
<Grid x:Name="LayoutRoot">
    <Button Style="{DynamicResource ButtonStyle1}"/>
    <TextBlock
        x:Name="NodeName"
        x:FieldModifier="public"
        Text="Property"
        Margin="8"
        HorizontalAlignment="Center"
        VerticalAlignment="Center"
        TextWrapping="Wrap" 
        TextAlignment="Center"
        FontFamily="Segoe Print"
        FontWeight="Bold" 
        Foreground="White"
        FontSize="40"/>
</Grid>

将依赖项属性附加到用户控件

为什么不将Fill绑定到 Button 的 Background 属性:

<ControlTemplate TargetType="{x:Type Button}"> 
    <Grid> 
        <Rectangle Stroke="Black" Fill="{TemplateBinding Background}" /> 
        ...
    </Grid> 
    ...
</ControlTemplate> 

然后像这样设置Background

<Button Style="{DynamicResource ButtonStyle1}">
    <Button.Background>
        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">   
            <GradientStop Color="#FF48B6E4" Offset="0.013"/>   
            <GradientStop Color="#FF091D8D" Offset="1"/>   
        </LinearGradientBrush>   
    </Button.Background>
</Button> 

如果你有自己的依赖属性,叫做MyProperty在你的UserControl中注册,你可以这样绑定它:

...
<Rectangle Stroke="Black" Fill="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Parent.Parent.MyProperty}" />
...

无需进行其他更改。

这会将 Fill 属性绑定到样式分配到的控件的父级的父级,在本例中为UserControl本身。

使用此方法,您不仅可以将其绑定到UserControl的属性,还可以绑定到其他控件的属性。

我会在你的 YourUserControl 视图上创建一个依赖属性,如下所示(为了简洁起见,我删除了一些标记):

<UserControl.Resources>
<Style x:Key="ButtonStyle1" TargetType="{x:Type Button}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Grid>
                    <Rectangle Stroke="Black" Fill="{TemplateBinding Background}">
                    </Rectangle>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Grid x:Name="LayoutRoot">
    <Button Style="{DynamicResource ButtonStyle1}" Background="{Binding DynamicColor}"/>
</Grid>

然后在 YourUserControl.xaml 中.cs您可以创建依赖项属性:

    private My_ViewModel _viewModel
    {
        get { return this.DataContext as My_ViewModel; }
    }
    public LinearGradientBrush DynamicColor
    {
        get { return (string)GetValue(DynamicColorProperty); }
        set { SetValue(DynamicColorProperty, value); }
    }
    public static readonly DependencyProperty DynamicColorProperty =
        DependencyProperty.Register("DynamicColor", typeof(LinearGradientBrush), typeof(YourUserControl),
        new PropertyMetadata(new PropertyChangedCallback(OnDynamicColorPropertyChanged)));
    private static void OnDynamicColorPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        ((YourUserControl)d).OnTrackerInstanceChanged(e);
    }
    protected virtual void OnDynamicColorPropertyChanged(DependencyPropertyChangedEventArgs e)
    {
        this._viewModel.DynamicColor = e.NewValue;
    }
public class My_ViewModel : INotifyPropertyChanged
{
    public LinearGradientBrush DynamicColor
    {
        get { return dynamicColor; }
        set 
        { 
            if(dynamicColor != value)
            {
                dynamicColor = value;
                OnPropertyChanged("DynamicColor");
            }
        }
    }
    private LinearGradientBrush dynamicColor;
}

此方法使您可以完全控制 DynamicColor 属性的值,并允许您能够有效地对行为进行单元测试。