我有以下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功能中的每一个中定义属性,但我是寻找更好的方法,不需要在代码中重复,有人可以指导我这样做吗?
答案 0 :(得分:1)
这是你的UserControl:
<UserControl ...>
<StackPanel Orientation="Horizontal">
<Image Source="{Binding ImageSource}" Width="16"/>
<TextBlock Text="{Binding Text}" />
</StackPanel>
</UserControl>
及其代码:
除了Text
和ImageSource
之外,还有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中:
每个Button
或ToggleButton
都将其附加属性StackCtlState
设置为所需的值,并将其样式设置为Button
或{{1}的样式之一}。
然后这些样式以正确的方式向样式化按钮/ togglebutton的内容添加ToggleButton
,以便可以重用资源。 (如果您只设置内容而不设置模板,则只会显示上一个StackedImageTextCtl
或上一个Button
的内容)添加的ToggleButton
的{{1}}等于StackedImageTextCtl
到State
TemplatedParent
或Button
的附加值。
最后,该样式使用ToggleButton
根据Trigger
的{{1}}设置文字和图片的值。
State