C# 将属性绑定到父属性的值

C# 将属性绑定到父属性的值,c#,wpf,xaml,C#,Wpf,Xaml,我的Google Fu今天很低,因为我找不到这个非常琐碎的问题的答案: 我正在XAML中创建自己的自定义控件,该控件继承自UserControl。在它里面我有一个网格和一些文本块s 现在,我希望使用我的控件的人能够在我的控件上设置属性Background。然后我想使用该Background值在我的网格上设置Background属性 下面是我的XAML以及我的最新尝试: <!-- MainPage.xaml --> <Page> <!-- snipped all na

我的Google Fu今天很低,因为我找不到这个非常琐碎的问题的答案:

我正在XAML中创建自己的自定义控件,该控件继承自
UserControl
。在它里面我有一个
网格
和一些
文本块
s

现在,我希望使用我的控件的人能够在我的控件上设置属性
Background
。然后我想使用该
Background
值在我的
网格上设置
Background
属性

下面是我的XAML以及我的最新尝试:

<!-- MainPage.xaml -->
<Page> <!-- snipped all namespace-stuff -->
  <local:Foo Background="Red" Foreground="White"/>
</Page>

和自定义控件:

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

在TextBlock的绑定上使用RelativeSource以使用父网格中的属性

e、 g


在TextBlock的绑定上使用RelativeSource以使用父网格中的属性

e、 g

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


完整示例:

<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>

正文

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


完整示例:

<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>

正文

在类型“RelativeSource”中找不到属性“AncestorType”
在类型“RelativeSource”中找不到属性“AncestorType”
它在
用户控件的设计器中工作
但在使用
用户控件的设计器中或在运行时都没有颜色。添加了XAML。前台
似乎是继承的,因此无需任何绑定即可工作。所以我从我的问题中删除了这一部分。不过,
网格上的
背景
仍然存在问题。对我来说很有效。即使我放下了装订。背景和前景仍然是继承的。你到底有什么问题?背景不符合用户控件上的指定?我用您的代码创建了一个新项目,它可以工作,所以问题在我这边。但我会弄明白的。你的代码至少可以工作,所以我接受了你的答案。:)它确实在
UserControl
的设计器中工作,但是我在使用
UserControl
的控件设计器中或在运行时都没有得到颜色。添加了XAML。前台
似乎是继承的,因此无需任何绑定即可工作。所以我从我的问题中删除了这一部分。不过,
网格上的
背景
仍然存在问题。对我来说很有效。即使我放下了装订。背景和前景仍然是继承的。你到底有什么问题?背景不符合用户控件上的指定?我用您的代码创建了一个新项目,它可以工作,所以问题在我这边。但我会弄明白的。你的代码至少可以工作,所以我接受了你的答案。:)你也可以发布一个截图吗?你也可以发布一个截图吗?
<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>