【问题标题】:WPF grid across user controls?跨用户控件的 WPF 网格?
【发布时间】:2010-10-31 02:31:27
【问题描述】:

在 ASP.NET 中,我可以让用户控件在页面上的表格中占据多个单元格:

用户控件1:

<tr>
  <td>foo</td>
  <td>bar</td>
</tr>

第1页:

<table>
  <put a UserControl1 here/>
  <put another UserControl1 here/>
</table>

并自动调整列宽以适应最大的用户控件。

这也可以在 WPF 中完成吗?怎么样?

我认为用户控件不能做到这一点,我必须创建一个简单的类而不是用户控件,它具有将所有内容添加到网格的方法。但是那样一切都应该通过代码来完成,xaml在这里就没用了。

【问题讨论】:

    标签: wpf grid


    【解决方案1】:

    不,你不能在 XAML 中做类似的事情。

    ASP.NET 用户控件发出 HTML。您的用户控件为表格行发出 HTML。鉴于发出的标记在表单的标记中是有意义的,您可以做的事情是可能的,但不是很好的做法,因为控件完全取决于页面中的标记才能产生任何意义。此外,页面中控件的大小和位置应该是页面的责任,而不是控件的责任。这都是关于封装的。

    当然,WPF 控件不会发出标记。相反,它们在 WPF 窗口/页面中实例化,并作为窗口中的可视控件存在。完全不同的情况。

    要使您的控件跨越多个列,您应该使用 Grid.ColumnSpan 扩展属性,如下所示:

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <MyControl Grid.ColumnSpan="2" Name="myControl">Button</Button>
    </Grid>
    

    【讨论】:

      【解决方案2】:

      我不建议让用户控件定义它应该如何占据主机中的表格/网格单元格。承载用户控件的页面/窗口应定义用户控件的显示方式。这样您就可以将用户控件放置在 WPF 网格中,并使用 Grid.ColumnSpan 属性使用户控件在网格中占据多个列。

      控件最终将变得更加可用,并且保持了合理性的分离。

      【讨论】:

      • 您能否举一个代码示例说明如何实现上述目标?
      【解决方案3】:

      我找到了答案here

      可以使用 SharedSizeGroup 和 Grid.IsSharedSizeScope 来完成。

      UserControl1.xaml:

      <UserControl x:Class="WpfApplication1.UserControl1"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
          <Grid>
              <Grid.ColumnDefinitions>
                  <ColumnDefinition SharedSizeGroup="SharedSizeGroup1"/>
                  <ColumnDefinition SharedSizeGroup="SharedSizeGroup2"/>
              </Grid.ColumnDefinitions>
              <Label Name="Label1" Grid.Column="0">Label1</Label>
              <Label Name="Label2" Grid.Column="1">Label2</Label>
          </Grid>
      </UserControl>
      

      Window1.xaml:

      <Window x:Class="WpfApplication1.Window1"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:y="clr-namespace:WpfApplication1">
          <Grid Grid.IsSharedSizeScope="True">
              <Grid.RowDefinitions>
                  <RowDefinition/>
                  <RowDefinition/>
              </Grid.RowDefinitions>
              <y:UserControl1 Grid.Row="0" x:Name="UserControl1A"/>
              <y:UserControl1 Grid.Row="1" x:Name="UserControl1B"/>
          </Grid>
      </Window>
      

      Window1.xaml.cs:

      using System.Windows;
      
      namespace WpfApplication1
      {
          public partial class Window1 : Window
          {
              public Window1()
              {
                  InitializeComponent();
                  UserControl1A.Label1.Content = "Label1WithLongText";
              }
          }
      }
      

      【讨论】:

      猜你喜欢
      • 2011-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-15
      • 2016-06-14
      • 2023-03-11
      • 2017-03-16
      相关资源
      最近更新 更多