Attached Properties

懵懂的女人 提交于 2019-11-26 16:56:43

问题


I am a little confused about WPF attached properties. When you use an attached property that attached property can only be read and used by the class that defines it correct? For example if I wanted to use some attached property as a hover color on a button, can I get the attached property value from the button's template, and will I be able access the attached property from the button to set the hoover color?


回答1:


Have you read the overview? If not, do it.

Attached properties, like dependency properties, just register another key that can be used in the properties dictionary of controls. You can set values anywhere and you can retrieve them anywhere, they are not restricted by type. This means that you may only want it to be set on Buttons but it can be set on TextBoxes too.

Every control has its own dictionary of property keys and values, an attached property allows you to write a value to those dictionaries using a new key. As those dictionaries are independent they can have separate values for the same property which is set and accessed via the static field property declaration.

As those properties are attached you will have to get the values via the GetValue (as the classes cannot provide a CLR-wrapper themselves).




回答2:


Adding to the answer from H.B. using an example:

For example if I wanted to use some attached property as a hover color on a button, can I get the attached property value from the button's template, and will I be able access the attached property from the button to set the hover color?

Yes, you sure can. Say that you have an Attached Property called HoverBrush defined in a class called SomeClass, you can set the value on the instance and bind to it in the template

<StackPanel>
    <StackPanel.Resources>
        <ControlTemplate x:Key="MyButtonTemplate" TargetType="{x:Type Button}">
            <Border x:Name="border" Background="Gray">
                <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter TargetName="border"
                            Property="Background"
                            Value="{Binding RelativeSource={RelativeSource TemplatedParent},
                                            Path=(local:SomeClass.HoverBrush)}"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </StackPanel.Resources>
    <Button Content="Blue Hover"
            local:SomeClass.HoverBrush="Blue"
            Template="{StaticResource MyButtonTemplate}"/>
    <Button Content="Green Hover"
            local:SomeClass.HoverBrush="Green"
            Template="{StaticResource MyButtonTemplate}"/>
</StackPanel>

The attached property in question is defined like this

public class SomeClass
{
    public static DependencyProperty HoverBrushProperty =
        DependencyProperty.RegisterAttached("HoverBrush",
                                            typeof(Brush),
                                            typeof(SomeClass),
                                            new PropertyMetadata(null));
    public static void SetHoverBrush(DependencyObject obj, Brush value)
    {
        obj.SetValue(HoverBrushProperty, value);
    }
    public static Brush GetHoverBrush(DependencyObject obj)
    {
        return (Brush)obj.GetValue(HoverBrushProperty);
    }
}


来源:https://stackoverflow.com/questions/7148946/attached-properties

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!