WPF UserControl创建多个布局选择

时间:2013-05-12 22:35:15

标签: wpf user-controls templatebinding

我有一个UserControl,我定义了各种属性,因此我可以为屏幕上的每个副本自定义它。我有一个使用LinearGradientBrush填充的路径。目前,这是硬编码到XAML中。我已经将路径控件的宽度和可见性作为依赖项对象,并且可以轻松地修改它们:

<Path 
    Visibility="{TemplateBinding PathAVisibility}"
    Width="{TemplateBinding PathALength}">
        <LinearGradientBrush EndPoint="0,0.5" MappingMode="RelativeToBoundingBox" StartPoint="1,0.5">
            <GradientStop Color="#07FFFFFF" Offset="0.812"/>
            <GradientStop Color="Red"/>
            <GradientStop Color="#00000000" Offset="0.993"/>
            <GradientStop Color="#FF956666" Offset="0.62"/>
        </LinearGradientBrush>...

我想要做的是创建一些渐变作为选项,然后我可以在WPF XAML设计器中选择它们作为属性。像“GradA”这样的东西是红色,“GradB”是蓝色,但没有透明度等。

使用Visibility,我可以在设计视图中看到“Visible / Hidden / Collapsed”作为可供选择的选项,这就是我要追求的东西。

这就是我被困住的地方。我甚至不知道这会被称为什么,或者如何接近它。

我应该向哪个方向寻找指示?

2 个答案:

答案 0 :(得分:0)

您可以使用enumXaml中提供所需的固定值,然后您可以使用PropertyChangedCallback enum上的DependencyProperty进行更改Brush

这是非常快的例子。

代码:

public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();
        DataContext = this;
    }

    public BrushType BrushType
    {
        get { return (BrushType)GetValue(BrushTypeProperty); }
        set { SetValue(BrushTypeProperty, value); }
    }

    // Using a DependencyProperty as the backing store for BrushType.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty BrushTypeProperty =
        DependencyProperty.Register("BrushType", typeof(BrushType), typeof(UserControl1)
        , new PropertyMetadata(BrushType.None, new PropertyChangedCallback(OnBrushTypeChanged)));

    private static void OnBrushTypeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var userControl = d as UserControl1;
        if (e.NewValue is BrushType)
        {
            userControl.MyBrush = userControl.FindResource(e.NewValue.ToString()) as Brush;
        }
    }

    public Brush MyBrush
    {
        get { return (Brush)GetValue(MyBrushProperty); }
        set { SetValue(MyBrushProperty, value); }
    }

    // Using a DependencyProperty as the backing store for MyBrush.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty MyBrushProperty =
        DependencyProperty.Register("MyBrush", typeof(Brush), typeof(UserControl1), new PropertyMetadata(null));

}

public enum BrushType
{
    None,
    BrushA,
    BrushB,
    BrushC
}

的Xaml:

<UserControl x:Class="WPFListBoxGroupTest.UserControl1"
             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">
    <UserControl.Resources>
        <SolidColorBrush x:Key="BrushA" Color="Red" />
        <SolidColorBrush x:Key="BrushB" Color="Yellow" />
        <SolidColorBrush x:Key="BrushC" Color="Blue" />
    </UserControl.Resources>

    <Grid Background="{Binding MyBrush}" />

</UserControl>

用法:

<StackPanel Orientation="Horizontal">
    <local:UserControl1 BrushType="BrushA" />
    <local:UserControl1 BrushType="BrushB" />
    <local:UserControl1 BrushType="BrushC" />
</StackPanel>

结果:

enter image description here

答案 1 :(得分:0)

可以最大限度地减少编码工作的一个不错的功能是 视觉状态 。阅读他们here。还有其他机制来实现这一目标。样式,模板和触发器的组合也可以使用,但是更难以访问组件代码中的UI元素。