WPF network through user controls?

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

UserControl1:

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

Page1:

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

and the column width is automatically adjusted to fit the largest user control.

Can this be done in WPF? How?

I think user controls cannot do this, and I have to create a simple class instead of a user control that has a way to add everything to the grid. But in this way everything should be done using code, here xaml is useless.

+3
source share
3 answers

I found the answer 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";
        }
    }
}
+7

, 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>
+1

, , / . /, , , . , WPF Grid.ColumnSpan, .

Ultimately, the control will be much more convenient, and the separation of portability remains.

0
source

Source: https://habr.com/ru/post/1709708/


All Articles