2013-05-12 50 views
0

我有一個用戶控件,我有各種屬性定義,這樣我就可以自定義爲每個屏幕上的內容拷貝。我有一個使用LinearGradientBrush填充的路徑。目前這被硬編碼到XAML中。我已經有路徑控制的依賴對象的寬度和可見性,並可以很容易地修改這些:WPF用戶控件創建多個佈局選擇

<Path 
    Visibility="{TemplateBinding PathAVisibility}" 
    Width="{TemplateBinding PathALength}"> 
     <LinearGradientBrush EndPoint="0,0.5" MappingMode="RelativeToBoundingBox" StartPoint="1,0.5"> 
      <GradientStop Color="#07FFFFFF" Offset="0.812"/> 
      <GradientStop Color="Red"/> 
      <GradientStop Color="#00000000" Offset="0.993"/> 
      <GradientStop Color="#FF956666" Offset="0.62"/> 
     </LinearGradientBrush>... 

我希望做的是創建一些梯度作爲選項,然後我就可以選擇在性能WPF XAML設計師。類似於「GradA」的是「紅色」,「GradB」是藍色,但沒有透明度等。

通過可見性,我可以看到「可見/隱藏/摺疊」設計視圖,這是我追求的那種東西。

這是我堅持。我甚至不知道這將被稱爲,或如何處理它。

哪個方向我應該找任何指針?

回答

0

您可以使用enum爲您提供在Xaml想要的固定值,那麼你可以使用PropertyChangedCallbackenumDependencyProperty改變Brush

這裏是一個非常快例子。

代碼:

public partial class UserControl1 : UserControl 
{ 
    public UserControl1() 
    { 
     InitializeComponent(); 
     DataContext = this; 
    } 

    public BrushType BrushType 
    { 
     get { return (BrushType)GetValue(BrushTypeProperty); } 
     set { SetValue(BrushTypeProperty, value); } 
    } 

    // Using a DependencyProperty as the backing store for BrushType. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty BrushTypeProperty = 
     DependencyProperty.Register("BrushType", typeof(BrushType), typeof(UserControl1) 
     , new PropertyMetadata(BrushType.None, new PropertyChangedCallback(OnBrushTypeChanged))); 

    private static void OnBrushTypeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 
     var userControl = d as UserControl1; 
     if (e.NewValue is BrushType) 
     { 
      userControl.MyBrush = userControl.FindResource(e.NewValue.ToString()) as Brush; 
     } 
    } 

    public Brush MyBrush 
    { 
     get { return (Brush)GetValue(MyBrushProperty); } 
     set { SetValue(MyBrushProperty, value); } 
    } 

    // Using a DependencyProperty as the backing store for MyBrush. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty MyBrushProperty = 
     DependencyProperty.Register("MyBrush", typeof(Brush), typeof(UserControl1), new PropertyMetadata(null)); 

} 

public enum BrushType 
{ 
    None, 
    BrushA, 
    BrushB, 
    BrushC 
} 

的XAML:

<UserControl x:Class="WPFListBoxGroupTest.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" 
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"> 
    <UserControl.Resources> 
     <SolidColorBrush x:Key="BrushA" Color="Red" /> 
     <SolidColorBrush x:Key="BrushB" Color="Yellow" /> 
     <SolidColorBrush x:Key="BrushC" Color="Blue" /> 
    </UserControl.Resources> 

    <Grid Background="{Binding MyBrush}" /> 

</UserControl> 

用法:

<StackPanel Orientation="Horizontal"> 
    <local:UserControl1 BrushType="BrushA" /> 
    <local:UserControl1 BrushType="BrushB" /> 
    <local:UserControl1 BrushType="BrushC" /> 
</StackPanel> 

結果:

enter image description here

這裏
+0

好了,我就在想,我有一個用戶控件錯了,但我不我其實有一個資源字典,我則有基於控制。我在你的代碼中進行了探索,這幾乎是一種享受。未找到BrushA/B/C資源。如果我將它們放在Page級別作爲頁面資源,那麼它會編譯,但不會設置顏色。我會將你的答案標記爲正確的,因爲它符合我的要求。我的錯我把電線交叉了。你能建議我應該把SolidcolorBrush資源放在資源字典中嗎?非常感謝你花了這麼多時間。 – MikeyTT 2013-05-16 22:21:21

+0

如果您僅使用路徑上的畫筆,然後將其放在中或父容器中,如果您不使用UserControl,則可以將畫筆放置在您的控件的任何位置。 FrameworkElements擁有自己的資源 – 2013-05-16 22:30:32

+0

謝謝。我會在週末對此進行更多的研究,看看它對我的影響。 – MikeyTT 2013-05-17 08:39:50

0

一個不錯的功能,可以減少你的編碼工作量是可視狀態。閱讀關於他們here。還有其他機制可以實現這一點。樣式,模板和觸發器的組合也可以工作,但使得訪問後面的組件代碼中的UI元素變得更加困難。

+0

謝謝你。它可能會幫助我,當我來做一個我正在計劃的圖像動畫。 – MikeyTT 2013-05-18 20:04:33