【问题标题】:How can I see default DependencyProperty value in design time when I'm using binding使用绑定时如何在设计时查看默认的 DependencyProperty 值
【发布时间】:2017-08-09 09:11:50
【问题描述】:

我在 UserControl 上有一些要着色的多边形:

<LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
    <GradientStop Color="{Binding Color}" Offset="0"/>
    <GradientStop Color="{Binding SecondaryColor}" Offset="0.5"/>
    <GradientStop Color="{Binding Color}" Offset="1"/>
</LinearGradientBrush>

这是我的数据上下文:

<DataContext="{Binding RelativeSource={RelativeSource Self}}">

这是我的依赖属性,默认值在PropertyMetadata中定义:

[Category("Silo - appearance")]
public Color Color
{
    get { return (Color)GetValue(ColorProperty); }
    set { SetValue(ColorProperty, value); }
}
public static readonly DependencyProperty ColorProperty =
    DependencyProperty.Register("Color", typeof(Color), typeof(Silo), 
        new PropertyMetadata((Color)Color.FromArgb(255,0x27, 0x77, 0x9E)));

我把这个LinearGradientBrush 放在哪里的PolygonUserControl 设计时是透明的。

我尝试重建解决方案,但没有任何区别。

  1. 为什么我的默认值没有在设计时应用?

  2. 如何才能看到(在设计时)PropertyMetadata 中定义的默认颜色?

【问题讨论】:

  • 我知道的唯一方法是将依赖属性放在基类中,然后从该基类继承您的控件。然后它将在设计器中工作。不幸的是,我并不完全清楚为什么。
  • 我认为如何(以及何时)评估绑定的方式在这方面起着重要作用......
  • 如果你把属性放在父类中,它对你也有用吗?
  • 无论如何,我认为最好的方法是提供设计时数据上下文(具有 Color 和 SecondaryColor 常规、非依赖属性的单独类)并使用它。
  • 我的 UserControl 的父类?

标签: c# wpf xaml binding default-value


【解决方案1】:

我知道解决这个问题的一种方法不是很好,但似乎有效。您可以将依赖项属性移动到父类并从该类继承您的用户控件。例如:

public class Parent : UserControl
{
    [Category("Silo - appearance")]
    public Color Color
    {
        get { return (Color)GetValue(ColorProperty); }
        set { SetValue(ColorProperty, value); }
    }
    public static readonly DependencyProperty ColorProperty =
        DependencyProperty.Register("Color", typeof(Color), typeof(Parent),
            new PropertyMetadata((Color)Color.FromArgb(255, 0x1A, 0x17, 0xA)));

    public Color SecondaryColor
    {
        get { return (Color)GetValue(SecondaryColorProperty); }
        set { SetValue(SecondaryColorProperty, value); }
    }

    public static readonly DependencyProperty SecondaryColorProperty =
        DependencyProperty.Register("SecondaryColor", typeof(Color), typeof(Parent), new PropertyMetadata(Color.FromArgb(255, 0x47, 0x17, 0x9E)));
}

public partial class UserControl1 : Parent
{
    public UserControl1()
    {
        InitializeComponent();
    }       
}

然后在 xaml 中:

<local:Parent x:Class="WpfApplication1.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"
             xmlns:local="clr-namespace:WpfApplication1"
             mc:Ignorable="d"
             d:DesignHeight="300"
             d:DesignWidth="300"
              DataContext="{Binding RelativeSource={RelativeSource Self}}">
    <Rectangle Height="300" Width="300">
        <Rectangle.Fill>
            <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
                <GradientStop Color="{Binding Color}" Offset="0"/>
                <GradientStop Color="{Binding SecondaryColor}" Offset="0.5"/>
                <GradientStop Color="{Binding Color}" Offset="1"/>
            </LinearGradientBrush>
        </Rectangle.Fill>
    </Rectangle>
</local:Parent>

我不完全确定它为什么会起作用,但我的猜测是:wpf 设计器根本不会在您的控件中运行代码。众所周知,它不会运行 UserControl1 的构造函数,而且它似乎也不会在那里执行其他代码(例如依赖属性的静态字段初始化程序)。但是,如本例所示,它将实例化父类。可能它会动态创建新控件而无需其他代码,并从您的控件继承的类中继承它。 WPF 设计器究竟是如何工作的并没有很好的记录(如果有的话),所以我们只能猜测。

替代(我认为更好)的方法将只是使用设计时数据上下文:

public class UserControlDesignContext {
    public Color Color { get; set; } = Color.FromArgb(255, 0x11, 0x17, 0xA);
    public Color SecondaryColor { get; set; } = Color.FromArgb(255, 0x47, 0x17, 0x9E);
}

然后在 xaml 中:

<UserControl x:Class="WpfApplication1.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"
             xmlns:local="clr-namespace:WpfApplication1"
             mc:Ignorable="d"
             d:DesignHeight="300"
             d:DesignWidth="300"
              d:DataContext="{d:DesignInstance local:UserControlDesignContext, IsDesignTimeCreatable=True}"
              DataContext="{Binding RelativeSource={RelativeSource Self}}">
    <Rectangle Height="300" Width="300">
        <Rectangle.Fill>
            <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
                <GradientStop Color="{Binding Color}" Offset="0"/>
                <GradientStop Color="{Binding SecondaryColor}" Offset="0.5"/>
                <GradientStop Color="{Binding Color}" Offset="1"/>
            </LinearGradientBrush>
        </Rectangle.Fill>
    </Rectangle>
</UserControl>

【讨论】:

    猜你喜欢
    • 2011-01-17
    • 2016-03-19
    • 2018-09-07
    • 2013-06-17
    • 1970-01-01
    • 2012-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多