2014-02-10 35 views
0

我已经创建了一个UserControl,该用户控件提供了一个带有圆角和各边阴影效果的边框面板。它的工作正常,除了当我在控件的实例上设置背景画笔属性时,它不仅填充内部边框元素,而且还应用到网格,因此我松散了圆角效果。如何仅将属性应用于UserControl的一个元素

<UserControl x:Class="MyApp.Controls.RoundedPanel" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     x:Name="userControl" mc:Ignorable="d"> 
    <Grid> 
     <Grid Margin="-6,-5,-12,-13"> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="20"/> 
       <ColumnDefinition Width="*"/> 
       <ColumnDefinition Width="27"/> 
      </Grid.ColumnDefinitions> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="20"/> 
       <RowDefinition Height="*"/> 
       <RowDefinition Height="27"/> 
      </Grid.RowDefinitions> 
      <Image Height="20" Width="20" Source="../Resources/Shadow Top Left.png" Stretch="Fill"/> 
      <Image Height="20" Grid.Column="1" Source="../Resources/Shadow Top.png" Stretch="Fill"/> 
      <Image Height="20" Width="27" Grid.Column="2" Source="../Resources/Shadow Top Right.png" Stretch="Fill"/> 
      <Image Width="27" Grid.Column="2" Grid.Row="1" Source="../Resources/Shadow Right.png" Stretch="Fill"/> 
      <Image Width="20" Grid.Row="1" Source="../Resources/Shadow Left.png" Stretch="Fill"/> 
      <Image Height="27" Grid.Column="1" Grid.Row="2" Source="../Resources/Shadow Bottom.png" Stretch="Fill" /> 
      <Image Height="27" Width="20" Grid.Row="2" Source="../Resources/Shadow Bottom Left.png" Stretch="Fill"/> 
      <Image Height="27" Width="27" Grid.Column="2" Grid.Row="2" Source="../Resources/Shadow Bottom Right.png" Stretch="Fill" Opacity="0.8" ClipToBounds="True"/> 
     </Grid> 
     <Border CornerRadius="12,12,12,12" VerticalAlignment="Stretch"/> 
    </Grid> 

我需要能够改变只是边境元素的背景刷来填充圆形面板内,控制不同的情况下会有不同的颜色,所以我不想让刷子硬编码。我似乎可以实现这一目标的唯一方法是在后面的代码中为控件添加一个新的DependencyProperty。

public partial class RoundedPanel : UserControl 
{ 
    public RoundedPanel() 
    { 
     InitializeComponent(); 
    } 

    /// <summary> 
    /// Identifies the InnerBackground dependency property. 
    /// </summary> 
    public static readonly DependencyProperty InnerBackgroundProperty = 
     DependencyProperty.Register(
      "InnerBackground", typeof(Brush), typeof(RoundedPanel)); 

    /// <summary> 
    /// Gets or sets the InnerBackground assigned to the control. 
    /// </summary> 
    public Brush InnerBackground 
    {   
     get { return (Brush)GetValue(InnerBackgroundProperty); } 
     set { SetValue(InnerBackgroundProperty, value); } 
    } 
} 

然后,我可以将新属性绑定到边框元素背景。

<Border CornerRadius="12,12,12,12" VerticalAlignment="Stretch" Background="{Binding InnerBackground, ElementName=userControl}" /> 

这工作得很好,但似乎这样做的一个相当混乱的方式(是整洁以某种方式能够覆盖现有的背景属性只适用于边界元素)。有没有更好的方式来做到这一点,我失踪或这是正确的方法?

回答

0

你可以简单地从一个RelativeSource BindingUserControl,就像这样:

<Border CornerRadius="12" Background="{Binding InnerBackground, 
    RelativeSource={RelativeSource AncestorType={x:Type YourPrefix:RoundedPanel}}}" /> 

UPDATE >>>

噢,对不起,我完全错过了最后一段你的问题。

我不知道的任何方式,你可以用它来从着色其Background停止UserControl ...你可以重用Background属性是这样的:

<Border CornerRadius="12" Background="{Binding Background, 
    RelativeSource={RelativeSource AncestorType={x:Type YourPrefix:RoundedPanel}}}" /> 

UserControl仍然会它的颜色也是整个Background,所以你不会看到它。然而,如果你宣布你的控制为CustomControl,那么你可以已经做到了这一点:

<Border CornerRadius="12" Background="{TemplateBinding Background}" /> 

请有Control Authoring Overview页面的一个良好的阅读MSDN上虽然之前你想想改变您的控件的基类。

+0

感谢您回复谢里登。你能否详细说明一下,我不明白如何改变为RelativeSource可以消除对新DependancyProperty的需求,这正是我真正想做的事情?或者你只是指一种更干净的方式来进行绑定? – Garry

相关问题