跨用户控件的WPF网格?

Csu*_*enő 3 wpf grid

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

的UserControl1:

<tr>
  <td>foo</td>
  <td>bar</td>
</tr>
Run Code Online (Sandbox Code Playgroud)

第1页:

<table>
  <put a UserControl1 here/>
  <put another UserControl1 here/>
</table>
Run Code Online (Sandbox Code Playgroud)

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

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

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

Csu*_*enő 7

我在这里找到了答案.

它可以使用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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

Window1.xaml.cs:

using System.Windows;

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