【问题标题】:How to put a border around an ItemsControl?如何在 ItemsControl 周围放置边框?
【发布时间】:2009-06-10 16:01:24
【问题描述】:

我在用户控件中有一个带有自定义面板的项目控件。用户控件的大小仅受父窗口大小的约束。

<UserControl>
  <Grid>
    <Border BorderBrush="DarkGray" BorderThickness="5">
      <ItemsControl ItemsSource="{Binding ActiveGame.Grid.CellsFlat}">
        <ItemsControl.ItemsPanel>
          <ItemsPanelTemplate>
            <Wpf:HexagonalPanel/>
          </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
          <DataTemplate>
            <Button />
          </DataTemplate>
        </ItemsControl.ItemTemplate>
      </ItemsControl>
    </Border>
  </Grid>
</UserControl>

我现在希望只在生成的面板周围绘制边框。 但它是围绕整个网格绘制的,或者可能更精确地以与网格相同的大小绘制。

我认为我在 HexagonalPanel 上正确实现了 MeasureOverride,它返回正确的大小,所以它不应该以该大小绘制边框吗?

我错过了什么?

【问题讨论】:

    标签: wpf xaml border itemscontrol


    【解决方案1】:

    您尚未在 ItemsControl 上设置大小,因此它的大小也会与其父级相同。尝试将ItemsControlHorizontalAlignmentVerticalAlignment 设置为Center;这将使ItemsControl 的实际大小与其内容相匹配。

    【讨论】:

    • 谢谢!有效! WPF布局系统我还是要学很多的
    • 我听到了。这需要一些时间来适应。
    【解决方案2】:

    ItemsControl 本身具有 BorderBrushBorderThickness 属性。你可能想要使用这些,因为ItemsControl 默认在其ControlTemplate 中有一个Border

    【讨论】:

      【解决方案3】:

      你可以实现这个,计算和更新新的尺寸值:

      public class MyPanel : Panel{
      protected override Size ArrangeOverride(Size finalSize){
      
      // calculate new size
      ........
      
       this.SetValue(WidthProperty, totalwidth);
        this.SetValue(HeightProperty, totalheight);
      
      return new Size(totalwidth, totalheight);
      }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-30
        • 2013-02-04
        • 1970-01-01
        • 2022-11-04
        相关资源
        最近更新 更多