2017-03-17 45 views
2

我有一些用戶控件多邊形,我要的顏色:我怎麼能看到設計時默認的DependencyProperty值當我使用綁定

<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這裏我把這個LinearGradientBrushUserControl設計時透明。

我試圖重建解決方案,但沒有任何區別。

  1. 爲什麼我的默認值不適用於設計時間?

  2. 我該怎麼做才能看到(在設計時間)在PropertyMetadata中定義的默認顏色?

+0

我所知道的唯一的辦法就是把依賴屬性中基類然後從該基類繼承你的控制。然後它會在設計師工作。不幸的是,我不完全清楚爲什麼。 – Evk

+0

我認爲如何(和何時)綁定評估在這方面扮演着重要角色...... – Kamil

+0

如果您將屬性放在父類中,它是否也適用於您? – Evk

回答

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設計不會在你的控制中運行的代碼在所有。它已經知道它不會運行你的構造函數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> 
相關問題