使用绑定时如何在设计时查看默认的 DependencyProperty 值
How can I see default DependencyProperty value in design time when I'm using binding
我在 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:
<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)));
我的 Polygon
我把这个 LinearGradientBrush
放在哪里在 UserControl
设计时是透明的。
我尝试重建解决方案,但没有任何区别。
为什么我的默认值在设计时没有应用?
我该怎么做才能看到(在设计时)在 PropertyMetadata
中定义的默认颜色?
我知道解决该问题的一种方法不是很好,但似乎有效。您可以将依赖项属性移至父级 class 并从该 class 继承您的用户控件。例如:
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
的构造函数,而且它似乎也不会在那里执行其他代码(比如你的依赖项 属性 的静态字段初始值设定项)。但是,它将实例化父 class,如本示例所示。可能它动态创建新控件而无需其他代码,并从您的控件继承的 class 继承它。 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>
我在 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:
<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)));
我的 Polygon
我把这个 LinearGradientBrush
放在哪里在 UserControl
设计时是透明的。
我尝试重建解决方案,但没有任何区别。
为什么我的默认值在设计时没有应用?
我该怎么做才能看到(在设计时)在
PropertyMetadata
中定义的默认颜色?
我知道解决该问题的一种方法不是很好,但似乎有效。您可以将依赖项属性移至父级 class 并从该 class 继承您的用户控件。例如:
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
的构造函数,而且它似乎也不会在那里执行其他代码(比如你的依赖项 属性 的静态字段初始值设定项)。但是,它将实例化父 class,如本示例所示。可能它动态创建新控件而无需其他代码,并从您的控件继承的 class 继承它。 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>