跨用户控件的 WPF 网格? [英] WPF grid across user controls?

查看:22
本文介绍了跨用户控件的 WPF 网格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In ASP.NET I could make a user control to occupy more than one cell in a table on the page:

用户控件1:

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

第1页:

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

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

and the column widths adjusted automatically to fit the largest user control.

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

Can this be done in WPF too ? How ?

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

I think that user controls can't do that, and I must create a simple class instead of a user control, which has a method to add everything to the grid. But that way everything should be done by code, xaml is useless here.

推荐答案

我找到了答案 这里.

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

It can be done with SharedSizeGroup and Grid.IsSharedSizeScope.

UserControl1.xaml:

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:

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:

Window1.xaml.cs:

using System.Windows;

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

这篇关于跨用户控件的 WPF 网格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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