当我使用绑定时,如何在设计时查看默认的 DependencyProperty 值

Kam*_*mil 3 c# wpf xaml binding default-value

我在 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>
Run Code Online (Sandbox Code Playgroud)

这是我的数据上下文:

<DataContext="{Binding RelativeSource={RelativeSource Self}}">
Run Code Online (Sandbox Code Playgroud)

这是我的依赖属性,其默认值定义在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)));
Run Code Online (Sandbox Code Playgroud)

Polygon把它放在哪里LinearGradientBrush在设计时是透明的UserControl

我尝试过重建解决方案,但没有什么区别。

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

  2. 我可以做什么来查看(在设计时)中定义的默认颜色PropertyMetadata

Evk*_*Evk 5

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

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();
    }       
}
Run Code Online (Sandbox Code Playgroud)

然后在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>
Run Code Online (Sandbox Code Playgroud)

我不完全确定它为什么起作用,但我的猜测是: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);
}
Run Code Online (Sandbox Code Playgroud)

然后在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>
Run Code Online (Sandbox Code Playgroud)