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

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

问题描述

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

UserControl1:

 < tr> 
< td> foo< / td>
< td>栏< / td>
< / tr>

第1页:

 <表> 
<把UserControl1放在这里/>
<把另一个UserControl1放在这里/>
< / table>

,并且列宽自动调整以适合最大的用户控制。



这也可以在WPF中完成吗?如何?



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



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

UserControl1.xaml:

 < UserControl x:Class =WpfApplication1.UserControl1
xmlns =http://schemas.microsoft.com/winfx/2006/xaml/演示文稿
xmlns:x =http://schemas.microsoft.com/winfx/2006/xaml>
<网格>
< Grid.ColumnDefinitions>
< ColumnDefinition SharedSizeGroup =SharedSizeGroup1/>
< ColumnDefinition SharedSizeGroup =SharedSizeGroup2/>
< /Grid.ColumnDefinitions>
< Label Name =Label1Grid.Column =0> Label1< / Label>
< Label Name =Label2Grid.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 =0x:Name =UserControl1A/>
< y:UserControl1 Grid.Row =1x:Name =UserControl1B/>
< / Grid>
< / Window>

Window1.xaml.cs:

 使用System.Windows; 

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


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

UserControl1:

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

Page1:

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

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

Can this be done in WPF too ? How ?

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.

解决方案

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";
        }
    }
}

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

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