gpt4 book ai didi

wpf - 如何使另一个 StackPanel 的 StackPanel 宽度?

转载 作者:行者123 更新时间:2023-12-01 07:48:53 26 4
gpt4 key购买 nike

我有两个停靠面板,每个停靠面板都有一个左侧 StackPanel。

的宽度底部 StackPanel 由 的宽度决定文字 在里面。

的宽度顶部 StackPanel 应该是 相同作为 的宽度底部 堆栈面板。

我试图通过 ElementName 将顶部 StackPanel 的宽度绑定(bind)到底部 StackPanel 的宽度,但这不起作用。

如何使顶部宽度与底部宽度相同?

alt text

<StackPanel>
<DockPanel LastChildFill="True" Height="100" >
<StackPanel Width="{Binding ElementName=LeftMenuText, Path=Width}"
DockPanel.Dock="Left"
Background="Yellow">
<TextBlock
Text="This is some text."/>
</StackPanel>
<StackPanel DockPanel.Dock="Right"
Background="Orange">
</StackPanel>
</DockPanel>

<DockPanel
Height="3"
Background="Black"></DockPanel>

<DockPanel LastChildFill="True" Height="100">
<StackPanel Name="LeftMenuWrapper"
DockPanel.Dock="Left"
Background="Yellow">
<TextBlock
Text="This is some text that is longer."/>
</StackPanel>
<StackPanel DockPanel.Dock="Right"
Background="Blue">
</StackPanel>
</DockPanel>
</StackPanel>

最佳答案

将其绑定(bind)到 LeftMenuWrapper 的 ActualWidth:

 <StackPanel>
<DockPanel LastChildFill="True" Height="100" >
<StackPanel Width="{Binding ElementName=LeftMenuWrapper, Path=ActualWidth}"
DockPanel.Dock="Left"
Background="Yellow">
<TextBlock
Text="This is some text."/>
</StackPanel>
<StackPanel DockPanel.Dock="Right"
Background="Orange">
</StackPanel>
</DockPanel>

<DockPanel
Height="3"
Background="Black"></DockPanel>

<DockPanel LastChildFill="True" Height="100">
<StackPanel Name="LeftMenuWrapper"
DockPanel.Dock="Left"
Background="Yellow">
<TextBlock
Text="This is some text that is longer."/>
</StackPanel>
<StackPanel DockPanel.Dock="Right"
Background="Blue">
</StackPanel>
</DockPanel>
</StackPanel>

只是为了增加你的武器库另一种方式来做到这一点。您还可以使用 Grid 的 IsSharedScope 属性:
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel Grid.IsSharedSizeScope="True">
<Grid Height="100">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" SharedSizeGroup="TextHolder"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Border Background="Yellow">
<TextBlock Text="This is some text."/>
</Border>
<Border Grid.Column="1" Background="Orange"/>
</Grid>
<Border Height="3" Background="Black"/>
<Grid Height="100">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" SharedSizeGroup="TextHolder"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Border Background="Yellow">
<TextBlock Text="This is some text that is longer."/>
</Border>
<Border Grid.Column="1" Background="Blue"/>
</Grid>
</StackPanel>
</Page>

关于wpf - 如何使另一个 StackPanel 的 StackPanel 宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1466588/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com