易依赖属性

时间:2011-08-23 12:27:01

标签: wpf vb.net xaml dependency-properties

我有一个名为“btn1”的按钮,我想通过xaml中的依赖属性更改它的内容,如下所示:

<UserControl:UserControl1 ButtonContents="Something"/>

这就是我所拥有的:

Public Class UserControl1 
    Public Shared ReadOnly ButtonContentsProperty As DependencyProperty =
        DependencyProperty.Register("ButtonContents",
                                    GetType(String),
                                    GetType(UserControl.UserControl1))

    Public Property ButtonContents() As Boolean
        Get
            Return GetValue(ButtonContentsProperty)
        End Get
        Set(ByVal value As Boolean)
            SetValue(ButtonContentsProperty, value)
        End Set
    End Property
End Class

但是依赖属性如何知道该怎么做?

3 个答案:

答案 0 :(得分:0)

注册属性时,您还可以定义PropertyChangedCallback方法,您可以在该方法中决定属性更改时要执行的操作。 更多信息herehere

答案 1 :(得分:0)

我通常会绑定UserControl的XAML中的值,例如

<UserControl ...
             Name="control">
    <!-- ... -->
    <Button Content="{Binding ButtonContents, ElementName=control}"/>
    <!-- ... -->
</UserControl>

答案 2 :(得分:0)

解决方案基于以下方法 - 按钮的内容被定义为属于按钮本身的资源。不幸的是,ResourceKey不是DP,因此无法绑定,我们创建了一个附加属性BindiableResourceKey,它为此存在。用户控件具有字符串类型的属性ButtonLook,其中包含要用作按钮内容的资源的名称。如果需要实现更复杂的链接逻辑,只需扩展附加的属性值更改处理程序。

以下是代码:

第1部分 - 用户控制:

<UserControl x:Class="ButtonContentBinding.AControl"
             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" 
             mc:Ignorable="d" 
             d:DesignHeight="300" 
             d:DesignWidth="300"
             xmlns:local="clr-namespace:ButtonContentBinding">
    <Grid>
        <Button local:BindableResourceControl.ResourceKey="{Binding ButtonLook}">
            <Button.Resources>
                <Rectangle x:Key="BlueRectangle"
                    Width="40" Height="40" Fill="Blue" />
                <Rectangle x:Key="GreenRectangle"
                    Width="40" Height="40" Fill="Green" />
            </Button.Resources>
        </Button>
    </Grid>
</UserControl>

第2部分 - 附属财产:

public class BindableResourceControl : DependencyObject
    {
        public static readonly DependencyProperty ResourceKeyProperty =
            DependencyProperty.RegisterAttached("ResourceKey",
            typeof(string),
            typeof(BindableResourceControl),
            new PropertyMetadata((x, y) =>
            {
                ContentControl contentControl = x as ContentControl;

                if (x != null)
                {
                    contentControl.Content = contentControl.Resources[y.NewValue];
                }
            }));

        public static void SetResourceKey(DependencyObject x, string y)
        {
            x.SetValue(BindableResourceControl.ResourceKeyProperty, y);
        }

        public static string GetResourceKey(DependencyObject x)
        {
            return (string)x.GetValue(BindableResourceControl.ResourceKeyProperty);
        }
    }

第3部分 - 消费者:

<Window x:Class="ButtonContentBinding.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" xmlns:my="clr-namespace:ButtonContentBinding">
    <Grid>
        <my:AControl ButtonLook="GreenRectangle"
                HorizontalAlignment="Left" Margin="0"      
                x:Name="aControl1" VerticalAlignment="Top" 
                Height="200" Width="200" />
    </Grid>
</Window>