在WrapPanel中同步WPF控件宽度 [英] Synchronizing WPF control widths in a WrapPanel

查看:169
本文介绍了在WrapPanel中同步WPF控件宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这种情况

<WrapPanel>
    <CheckBox>Really long name</CheckBox>
    <CheckBox>Short</CheckBox>
    <CheckBox>Longer again</CheckBox>
    <CheckBox>Foo</CheckBox>
    <Slider MinWidth="200" />
</WrapPanel>

我希望WrapPanel中的所有Checkbox都具有相同的宽度.

I want all the CheckBoxes inside the WrapPanel to be the same width.

添加以下内容几乎可以达到预期的效果

Adding the following almost accomplishes the desired effect

<WrapPanel.Resources>
    <Style TargetType="CheckBox" BasedOn="{StaticResource {x:Type CheckBox}}">
        <Setter Property="MinWidth" Value="75" />
    </Style>
</WrapPanel.Resources>

但是,我不想硬编码特定的宽度,而是让最大的CheckBox设置宽度(如果宽度> 75,则上面的方法也会失败).

However, I do not want to hardcode a specific width, rather let the largest CheckBox set the width (the above also fails if any width > 75).

滑块是独立的,应允许其大于CheckBoxes.

The Slider is independent and should be allowed to be larger than the CheckBoxes.

我不想使用带有IsSharedSizeScope的网格,因为我不希望使用硬编码的列数.

I do not want to use a Grid (with IsSharedSizeScope) since I do not want a hardcoded number of columns.

本文提出了一个有趣的解决方案,但对解决此问题而无需创建自定义控件或使用C#代码.

This article presents an interesting solution, but it would be nice to solve the problem without creating a custom control or using C# code.

做到这一点的最佳方法是什么,最好仅在XAML中?

What is the best way to do this, preferrably in XAML only?

推荐答案

我最初使用IsSharedSizeGroup进行了研究,但遇到了障碍,使其动态地应用于事物而不是显式包装项目.在这种情况下,从长远来看,用代码或其他基于代码的解决方案创建AttachedProperty可能比仅使用XAML的方法更好.但是,要创建纯XAML解决方案,我们可以使用

I originally looked at this using IsSharedSizeGroup but hit a roadblock with making it dynamically apply to things instead of explicitly wrapping items. In this case, creating an AttachedProperty in code or another code based solution may in the long run be better then a XAML only approach. However, to create a purely XAML solution we can make use of the SharedSizeGroup property on a ColumnDefinition to share the sizes of each element and then use set the IsSharedSizeScope property on the WrapPanel. Doing so will make all of the contents in the WrapPanel with the same SharedSizeGroup share their width for columns and height for rows. To wrap the ComboBoxes and possibly ComboBoxes that are not currently in the XAML but will be added to the WrapPanel, we can create a Style and re-template the ComboBox to bassicly wrap it with a Grid.

<WrapPanel Grid.IsSharedSizeScope="True">
  <WrapPanel.Resources>
    <Style TargetType="{x:Type CheckBox}">
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate TargetType="{x:Type CheckBox}">
            <Grid Background="LightBlue">
              <Grid.ColumnDefinitions>
                <ColumnDefinition SharedSizeGroup="WrapPannelGroup" />
              </Grid.ColumnDefinitions>
              <CheckBox Style="{x:Null}"
                        IsChecked="{TemplateBinding IsChecked}">
                <!--Other TemplateBindings-->
                <CheckBox.Content>
                  <ContentPresenter />
                </CheckBox.Content>
              </CheckBox>
            </Grid>
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>

  </WrapPanel.Resources>
  <CheckBox>Really long name</CheckBox>
  <CheckBox>Short</CheckBox>
  <CheckBox IsChecked="True">Longer again</CheckBox>
  <CheckBox>Foo</CheckBox>
  <Slider MinWidth="200" />
</WrapPanel>

在这里,我们将WrapPannel内部没有样式的所有CheckBox重新模板化,改为由Grid包围的CheckBox.但是,由于这个原因,我们需要重新绑定我们要维护的所有CheckBoxes属性.尽管这可能会很麻烦,但它也允许使用纯XAML方法.

Here we are re-templating all CheckBoxes without a style inside the WrapPannel to instead be CheckBoxes surrounded by a Grid. However, because of this we need to re-bind all of the CheckBoxes properties that we want to maintain. While that could become burdensome, it also allows for a pure XAML approach.

这篇关于在WrapPanel中同步WPF控件宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆