【问题标题】:How to make a StackPanel Width that of another StackPanel?如何使另一个 StackPanel 的 StackPanel 宽度?
【发布时间】:2010-11-30 19:06:47
【问题描述】:

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

bottom StackPanel 的宽度由 text 的宽度决定。

top StackPanel 的宽度应该与 bottom 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>

【问题讨论】:

    标签: wpf xaml width stackpanel dockpanel


    【解决方案1】:

    将其绑定到 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>
    

    【讨论】:

      【解决方案2】:

      您可以使用Grids 和SharedSizeGroup 代替DockPanels 来执行此操作。即

      <StackPanel Grid.IsSharedSizeScope="True">
          <Grid Height="100" >
      
              <Grid.ColumnDefinitions>
                  <ColumnDefinition Width="Auto" SharedSizeGroup="A"/>
                  <ColumnDefinition Width="*" />
              </Grid.ColumnDefinitions>
      
              <StackPanel Grid.Column="0" Width="{Binding ElementName=LeftMenuText, Path=Width}"
                                      DockPanel.Dock="Left"
                                      Background="Yellow">
                  <TextBlock
                          Text="This is some text."/>
              </StackPanel>
              <StackPanel Grid.Column="1" DockPanel.Dock="Right"
                                      Background="Orange">
              </StackPanel>
          </Grid>
      
          <DockPanel
            Height="3"
            Background="Black"></DockPanel>
      
          <Grid Height="100">
      
              <Grid.ColumnDefinitions>
                  <ColumnDefinition Width="Auto" SharedSizeGroup="A"/>
                  <ColumnDefinition Width="*" />
              </Grid.ColumnDefinitions>
      
              <StackPanel Grid.Column="0" Name="LeftMenuWrapper"
                                      DockPanel.Dock="Left"
                                      Background="Yellow">
                  <TextBlock
                                  Text="This is some text that is longer."/>
              </StackPanel>
              <StackPanel Grid.Column="1" DockPanel.Dock="Right"
                                      Background="Blue">
              </StackPanel>
          </Grid>
      </StackPanel>
      

      要记住的关键事项是为网格中的每一列提供一个具有相同名称的SharedSizeGroup(在本例中为“A”),并将Grid.IsSharedSizeScope="True" 添加到Grids 的共享父级( StackPanel 包含此示例中的 Grids)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-11-30
        • 2012-06-23
        • 1970-01-01
        • 1970-01-01
        • 2020-03-30
        • 2013-07-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多