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

时间:2012-04-11 16:11:59

标签: c# .net wpf xaml dependency-properties

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

<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>

3 个答案:

答案 0 :(得分:1)

如果您在MyProperty中注册了名为UserControl的自己的依赖项属性,则可以这样绑定:

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

无需其他更改。

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

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

答案 1 :(得分:1)

为什么不将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> 

答案 2 :(得分:1)

我会在你的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属性的值,并允许您有效地对行为进行单元测试。