嵌套用户控件中的WPF依赖项属性

时间:2013-11-10 12:04:40

标签: c# wpf xaml wpf-controls

我有以下WPF控件,用于显示带有图像的文本

XAML代码

<UserControl x:Class="WFWorkSpaceWPF.UserControls.StackedImageTextCtl"
         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" 
         Name="StackedImageText"
         >
<Grid>
    <StackPanel Orientation="Horizontal">
        <Image Source="{Binding ElementName=StackedImageText, Path=ImageSource}" />
        <TextBlock Text="{Binding ElementName=StackedImageText, Path=Text}" />
    </StackPanel>
</Grid>

CS

 public partial class StackedImageTextCtl : UserControl
{
    public StackedImageTextCtl()
    {
        InitializeComponent();
    }
    #region "Properties"
    public string Text
    {
        get { return (string)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }

    public static readonly DependencyProperty TextProperty =
      DependencyProperty.Register("Text", typeof(string), typeof(StackedImageTextCtl), new UIPropertyMetadata(""));

    public ImageSource ImageSource
    {
        get { return (ImageSource)GetValue(ImageSourceProperty); }
        set { SetValue(ImageSourceProperty, value); }
    }

    public static readonly DependencyProperty ImageSourceProperty =
       DependencyProperty.Register("ImageSource", typeof(ImageSource), typeof(StackedImageTextCtl), new UIPropertyMetadata(null));
    #endregion
}

在我的项目中,我希望在其他三个用户控件中重用此控件,它将作为这些控件的一部分添加,如您所见,StackedImageTextCtl公开了父用户控件需要的两个属性(文本和图像源)提供给它和这三个控件将从容器窗口中取出值抛出XAML,我知道其中一种方法是复制在这三个用户控件和使用AddOwner功能中的每一个中定义属性,但我是寻找更好的方法,不需要在代码中重复,有人可以指导我这样做吗?

1 个答案:

答案 0 :(得分:1)

这是你的UserControl:

<UserControl ...>
    <StackPanel Orientation="Horizontal">
        <Image Source="{Binding ImageSource}" Width="16"/>
        <TextBlock Text="{Binding Text}" />
    </StackPanel>
</UserControl>

及其代码:

除了TextImageSource之外,还有State代表此控件的Add/Edit/delete状态,并且StackCtlState是附加的属性,当附加到FrameworkElement时,它代表该控件的Add/Edit/delete状态。

    public StackedImageTextCtl()
    {
        InitializeComponent();
        DataContext = this;
    }
    //Text Dependency Property
    public string Text
    {
        get { return (string)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }
    public static readonly DependencyProperty TextProperty =
        DependencyProperty.Register("Text", typeof(string), typeof(StackedImageTextCtl), new UIPropertyMetadata(null));
    //ImageSource Dependency Property
    public ImageSource ImageSource
    {
        get { return (ImageSource)GetValue(ImageSourceProperty); }
        set { SetValue(ImageSourceProperty, value); }
    }
    public static readonly DependencyProperty ImageSourceProperty =
        DependencyProperty.Register("ImageSource", typeof(ImageSource), typeof(StackedImageTextCtl), new UIPropertyMetadata(null));
    //State Dependency Property
    public AddEditDelete State
    {
        get { return (AddEditDelete)GetValue(StateProperty); }
        set { SetValue(StateProperty, value); }
    }
    public static readonly DependencyProperty StateProperty =
        DependencyProperty.Register("State", typeof(AddEditDelete), typeof(StackedImageTextCtl), new UIPropertyMetadata(AddEditDelete.Add));

    public static AddEditDelete GetStackCtlState(DependencyObject obj)
    {
        return (AddEditDelete)obj.GetValue(StackCtlStateProperty);
    }

    public static void SetStackCtlState(DependencyObject obj, AddEditDelete value)
    {
        obj.SetValue(StackCtlStateProperty, value);
    }
    public static readonly DependencyProperty StackCtlStateProperty =
        DependencyProperty.RegisterAttached("StackCtlState", typeof(AddEditDelete), typeof(StackedImageTextCtl), new UIPropertyMetadata(AddEditDelete.Add));

我还定义了一个枚举:

public enum AddEditDelete { Add, Edit, Delete }

在Window xaml中:

每个ButtonToggleButton都将其附加属性StackCtlState设置为所需的值,并将其样式设置为Button或{{1}的样式之一}。

然后这些样式以正确的方式向样式化按钮/ togglebutton的内容添加ToggleButton,以便可以重用资源。 (如果您只设置内容而不设置模板,则只会显示上一个StackedImageTextCtl或上一个Button的内容)添加的ToggleButton的{​​{1}}等于StackedImageTextCtlState TemplatedParentButton的附加值。

最后,该样式使用ToggleButton根据Trigger的{​​{1}}设置文字和图片的值。

State