使用绑定时如何在设计时看到默认的DependencyProperty值 [英] How can I see default DependencyProperty value in design time when I'm using binding

查看:59
本文介绍了使用绑定时如何在设计时看到默认的DependencyProperty值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have some polygon on UserControl that I want to color:

<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:

This is my DataContext:

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

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

And this is my dependency property with default value defined in 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 Polygon UserControl 设计时是透明的.

My Polygon where I put this LinearGradientBrush is transparent in UserControl design time.

我试图重建解决方案,但是没有区别.

I have tried to rebuild solution, but there is no difference.

  1. 为什么在设计时未应用默认值?

如何查看(在设计时) PropertyMetadata 中定义的默认颜色?

What can I do to see (in design time) default color defined in PropertyMetadata?

推荐答案

我知道要解决的一种方法不是很好,但是似乎可以正常工作.您可以将依赖项属性移到父类,并从该类继承用户控件.例如:

One way I know to solve that is not very nice, but seems to be working. You can move your dependency properties to parent class and inherit your user control from that class. For example:

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 designer根本不在您的控件中运行代码.已经知道它将不会运行 UserControl1 的构造函数,并且似乎也不会在那里执行其他代码(例如,依赖项属性的静态字段初始化器).但是,它将如本示例所示实例化父类.它可能动态地创建了没有附加代码的新控件,并从您的控件继承的类继承了它.WPF设计器的确切工作方式尚不清楚(如果有的话),因此我们只能猜测.

I'm not completely certain why it works, but my guess is: wpf designer does not run code in your control at all. It is already known that it will not run constructor of your UserControl1, and it seems it will not execute other code there too (like static field initializers for your dependency property). However it will instantiate parent class as this example shows. Probably it dynamically creates new control with no additional code and inherits it from the class your control inherits. How exactly WPF designer works is not well documented (if at all), so we can only guess.

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

Alternative (and I think better) approach will be just use design-time data context:

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>

这篇关于使用绑定时如何在设计时看到默认的DependencyProperty值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆