C# 查找同级控件的属性

C# 查找同级控件的属性,c#,wpf,data-binding,C#,Wpf,Data Binding,Windows 10 Pro 64,VS 2019社区,WPF应用程序 我有一个应用程序,它有一个UserControl和两个成员,每个成员都是一个UserControl,它包含一个标题,标题是2-3行文本,下面有一个列表框。我的目标是将一个标题的高度设置为另一个标题的高度,我选择将一个标题绑定到另一个标题来实现这一点。但是,似乎我的相对资源没有解析,这导致尝试将高度设置为NaN,当然,这不会改变它。我尝试了许多不同的方法来表达相对资源,但没有一种有效。以下是最新消息: 这是主窗口: &

Windows 10 Pro 64,VS 2019社区,WPF应用程序

我有一个应用程序,它有一个UserControl和两个成员,每个成员都是一个UserControl,它包含一个标题,标题是2-3行文本,下面有一个列表框。我的目标是将一个标题的高度设置为另一个标题的高度,我选择将一个标题绑定到另一个标题来实现这一点。但是,似乎我的相对资源没有解析,这导致尝试将高度设置为NaN,当然,这不会改变它。我尝试了许多不同的方法来表达相对资源,但没有一种有效。以下是最新消息:

这是主窗口:

    <Window x:Class="MyApp.MainWindow"
        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"
        xmlns:local="clr-namespace:MyApp"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <local:MyParentControl x:Name="Daddy"/>
    </Grid>
</Window>

知道我做错了什么吗?

使用SharedSizeGroup来同步行的高度,并移除堆叠面板以支持网格行:




结果:

根据两个子控件的内容,我强烈建议只使用一个子UserControl,并使用MVVM用不同的数据填充它。这样就不需要维护两个基本相同的控件。

“我的目标是将一个收割台的高度设置为另一个收割台的高度”——这似乎是合理的。“我选择将一个绑定到另一个以实现这一点”——这一点都不合理。您不应该尝试将一个UI对象的属性绑定到另一个UI对象的属性。相反,您更喜欢WPF提供的丰富的内置布局机制数组。有很多种不同的方法可以用来实现你的目标,在我看来,贴出的答案是一个不错的选择。另见
<UserControl x:Class="MyApp.MyParentControl"
             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" 
             xmlns:local="clr-namespace:MyApp"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <StackPanel Orientation="Horizontal">
            <local:MySonControl x:Name="Sonny"/>
            <local:MyDaughterControl x:Name="Girlie"/>
        </StackPanel>
    </Grid>
</UserControl>
<UserControl x:Class="MyApp.MySonControl"
             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" 
             xmlns:local="clr-namespace:MyApp"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <StackPanel Orientation="Vertical">
            <StackPanel Orientation="Vertical" x:Name="Header">
                <TextBlock Text="Vertical space" HorizontalAlignment="Center"/>
                <TextBlock Text="Sonny's Stuff" HorizontalAlignment="Center"/>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="Column A" Margin="5,5,5,5"/>
                    <TextBlock Text="Column B" Margin="5,5,5,5"/>
                    <TextBlock Text="Column C" Margin="5,5,5,5"/>
                </StackPanel>
            </StackPanel>
            <ListBox x:Name="Stuff"/>
        </StackPanel>
    </Grid>
</UserControl>
<UserControl x:Class="MyApp.MyDaughterControl"
             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" 
             xmlns:local="clr-namespace:MyApp"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <StackPanel Orientation="Vertical">
            <StackPanel Orientation="Vertical" x:Name="Header"
                        Height="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:MyParentControl}, Path=Sonny.Header.ActualHeight}"
                        >
                <TextBlock Text="Girlie's Stuff" HorizontalAlignment="Center"/>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="Column A" Margin="5,5,5,5"/>
                    <TextBlock Text="Column B" Margin="5,5,5,5"/>
                    <TextBlock Text="Column C" Margin="5,5,5,5"/>
                </StackPanel>
            </StackPanel>
            <ListBox x:Name="Stuff"/>
        </StackPanel>
    </Grid>
</UserControl>
System.Windows.Data Error: 40 : BindingExpression path error: 'Sonny' property not found on 'object' ''MyParentControl' (Name='Daddy')'. BindingExpression:Path=Sonny.Header.ActualHeight; DataItem='MyParentControl' (Name='Daddy'); target element is 'StackPanel' (Name='Header'); target property is 'Height' (type 'Double')