我在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
?
答案 0 :(得分:3)
我知道要解决的一种方法并不是很好,但似乎有效。您可以将依赖项属性移动到父类,并从该类继承用户控件。例如:
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设计师的工作原理没有详细记录(如果有的话),所以我们只能猜测。
替代方案(我认为更好)方法只是使用设计时数据上下文:
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>