如何使 StackPanel 的宽度等于另一个 StackPanel 的宽度?
我有两个停靠面板,每个面板都有一个左侧 StackPanel。
底部 StackPanel 的宽度由其中文本 的宽度决定。
顶部 StackPanel 的宽度应与底部 StackPanel 的宽度相同。
我尝试通过 ElementName 将顶部 StackPanel 的宽度绑定到底部 StackPanel 的宽度,但这不起作用。
如何使顶部宽度与底部宽度相同?
<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>
I have two dockpanels which each have a left StackPanel.
The width of the bottom StackPanel is determined by the width of the text is in it.
The width of the top StackPanel should be the same as the width of the bottom StackPanel.
I've tried to bind the Width of the top StackPanel to the Width of the bottom StackPanel via ElementName but this doesn't work.
How can I make the top width the same as the bottom width?
<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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将其绑定到 LeftMenuWrapper 的 ActualWidth:
只是为了向您的武器库中添加另一种方法来执行此操作。您还可以使用 Grid 的 IsSharedScope 属性:
Bind it to ActualWidth of LeftMenuWrapper:
Just to add to your arsenal another way to do this. You can also use Grid's IsSharedScope property:
您可以使用带有
SharedSizeGroup
的Grid
而不是DockPanel
来完成此操作。即,要记住的关键事项是为网格内的每一列提供一个具有相同名称(本例中为“A”)的
SharedSizeGroup
,并添加Grid.IsSharedSizeScope="True"
code> 到Grid
的共享父级(本例中包含Grid
的StackPanel
)You can do this using
Grid
s with aSharedSizeGroup
instead ofDockPanel
s. I.e.The key things to remember are to give each column inside your grids a
SharedSizeGroup
with the same name ("A" in this example), and addGrid.IsSharedSizeScope="True"
to a shared parent of theGrid
s (theStackPanel
containing theGrid
s in this example)