绑定属性parent属性

绑定属性parent属性

问题描述:

我的谷歌赋的值今天是低的,因为我只是无法找到答案,这真的很重要的问题:绑定属性parent属性

我在XAML从UserControl继承创建自己的自定义控制。在里面我有一个Grid和一些TextBlock s。

现在,我希望使用我的控件的任何人都能够在我的控制下设置属性Background。然后我想使用Background值来设置我的Grid上的Background属性。

这里是我的XAML和我的最新尝试:

​​

而且自定义控件:

<!-- Foo.xaml --> 
<UserControl Name="UC"> <!-- snipped all namespace-stuff --> 
    <Grid Background="{Binding Path=Background, ElementName=UC}"> 
    <TextBlock Text="My custom control"/> 
    </Grid> 
</Page> 
+0

莫非你还张贴截图? – Onur

这个怎么样(背景网格作为练习留给读者......):

<UserControl Name="UC"> 

    <TextBlock Foreground="{Binding ElementName=UC, Path=Foreground}"/> 

</UserControl> 

完整的例子:

<Window x:Class="UnrelatedTests.Case8.Window1" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:case8="clr-namespace:UnrelatedTests.Case8" 
     Title="Window1" Height="300" Width="300"> 
    <Grid> 
     <case8:UserControl1 Background="Blue" Foreground="Red"/> 
    </Grid> 
</Window> 



<UserControl x:Class="UnrelatedTests.Case8.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" 

      Name="UC1" 
      > 
    <Grid> 
     <TextBlock Background="White" Foreground="{Binding ElementName=UC1, Path=Foreground}">Text</TextBlock> 
    </Grid> 
</UserControl> 

design and runtime view

+0

它可以在'UserControl'的设计器中工作,但是在使用'UserControl'的控件的设计器或运行时无法获得颜色。 –

+0

你能从“通话方”发布一些代码吗? – Onur

+0

添加了XAML。 '前景'似乎是被继承的,所以它可以在不需要绑定的情况下工作。所以我从我的问题中删除了这部分。尽管如此,“Grid”上的“背景”仍然存在问题。 –

使用的RelativeSource对TextBlock的使用属性从父电网的结合。

例如

{Binding Path=Background, RelativeSource={RelativeSource AncestorType={x:Type Grid}}} 
+0

'在'RelativeSource'类型中未找到'AncestorType'属性' –