WPF grid across user controls?

女生的网名这么多〃 提交于 2019-12-06 03:48:17

I've found the answer here.

It can be done with SharedSizeGroup and 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";
        }
    }
}

No you can't do anything like that in XAML.

ASP.NET user controls emit HTML. Your user control emits the HTML for a table row. Given that the emitted markup makes sense in the form's markup, what you do is possible, but not very good practice, since the control depends entirely on the markup in the page to make any sense at all. Besides, the size and placement of the control in the page should be the page's responsibility, not the control's. It's all about encapsulation.

WPF controls don't emit markup, of course. Rather, they are instantiated in the WPF window/page, and live as visual controls in the window. A different situation entirely.

To make your control span several columns, you should use the Grid.ColumnSpan extension attribute, like this:

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

I would not recommend having the user control define how it should occupy table/grid cells in a host. The page/window hosting the user control should define how the user control should be displayed. That way you can place the user controls in a WPF grid, and use Grid.ColumnSpan attribute to make the user control occupy multiple columns in the grid.

The control will end up being much more usable, and seperation of resonsability is kept.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!