C# Windows Phone-使用dependendy属性绑定solidcolorbrush属性

C# Windows Phone-使用dependendy属性绑定solidcolorbrush属性,c#,xaml,windows-phone-8,dependency-properties,C#,Xaml,Windows Phone 8,Dependency Properties,我有一个名为Tile的自定义控件,其中包含边框。 我希望能够在添加控件的MainPage.xaml中更改互动程序的边框背景色: 以下是我的tile类的代码: public static readonly DependencyProperty MyDpProperty = DependencyProperty.Register ("TileColor", typeof(SolidColorBrush), typeof(Tile),

我有一个名为Tile的自定义控件,其中包含边框。 我希望能够在添加控件的MainPage.xaml中更改互动程序的边框背景色:

以下是我的tile类的代码:

    public static readonly DependencyProperty MyDpProperty = DependencyProperty.Register
        ("TileColor",
         typeof(SolidColorBrush),
         typeof(Tile),
         new PropertyMetadata(default(SolidColorBrush)));

    public SolidColorBrush TileColor
    {
        get 
        {
            return (SolidColorBrush)this.GetValue(MyDpProperty);
        }
        set 
        {       
            this.SetValue(MyDpProperty, value);
        }
    }
以下是来自我的Tile CustomControl的xaml:

<Grid x:Name="LayoutRoot" Background="{StaticResource PhoneChromeBrush}">
    <Border Background="{Binding TileColor}"></Border>
</Grid> 

这是我的MainPage.xaml:

        <local:Tile Grid.Column="5">
            <local:Tile.TileColor>
                <SolidColorBrush Color="Beige"></SolidColorBrush>
            </local:Tile.TileColor>
        </local:Tile>

此时将显示平铺,但无法设置颜色。。
您能告诉我哪里错了吗?

您需要将
DependencyProperty
重命名为
TileColorProperty
(如@dkozl所说),并且您还需要在控件中使用
{TemplateBinding}

Themes\generic.xaml

<ResourceDictionary
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
         xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
         xmlns:local="clr-namespace:Silverlight80App">

  <Style TargetType="local:Tile">
    <Setter Property="TileColor" Value="Aquamarine"/>
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="local:Tile">
          <Rectangle Fill="{TemplateBinding TileColor}"/>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>
</ResourceDictionary>
用法

<StackPanel  xmlns:local="clr-namespace:Silverlight80App">
  <local:Tile HorizontalAlignment="Stretch" Height="10" />
  <local:Tile HorizontalAlignment="Stretch" Height="10" TileColor="Red"/>
</StackPanel>


您是否有可能在
UserControl
中执行类似于
DataContext=this
的操作?我怀疑是的,如果是这种情况,请删除它,给控件一些名称
,并使用它
ElementName
绑定内部控件,而不是使用它。不幸的是,我没有在代码中的任何地方使用DataContext=this。“改为在控件内部使用it ElementName绑定”是什么意思?如果不使用它,那么覆盖
DataContext
看起来不像是您的问题。另外,您的
DependencyProperty
应该被称为
TileColorProperty
,而不是
MyDpProperty
重命名DependencyProperty并没有改变行为。在没有看到您的代码的情况下,我无法准确地判断问题,但问题似乎是在绑定上下文中。默认情况下,它将在当前
DataContext
中查找
TileColor
,而不是在
UserControl
中。就像我说的,给
UserControl
一些名称
并使用
ElementName
binding
Background=“{binding ElementName=myControl,Path=TileColor}
<StackPanel  xmlns:local="clr-namespace:Silverlight80App">
  <local:Tile HorizontalAlignment="Stretch" Height="10" />
  <local:Tile HorizontalAlignment="Stretch" Height="10" TileColor="Red"/>
</StackPanel>