C# 使用MVVM动态创建WPF布局

C# 使用MVVM动态创建WPF布局,c#,wpf,layout,mvvm,rectangles,C#,Wpf,Layout,Mvvm,Rectangles,我试图在使用MVVM模式时,将视图放置到以矩形为基础的布局中的窗口 在WinForms中,我可以使用Rectage的宽度、高度、x和y来轻松定位控件,只需在控件上设置相同的属性即可 现在我正在使用MVVM将这段代码重写为wpf,我迷路了 这就是我想做的: 这是我认为可能有效的东西,但它没有 <Grid ShowGridLines="True"> <Grid.RowDefinitions> <RowDefinition />

我试图在使用MVVM模式时,将视图放置到以矩形为基础的布局中的窗口

在WinForms中,我可以使用Rectage的宽度、高度、x和y来轻松定位控件,只需在控件上设置相同的属性即可

现在我正在使用MVVM将这段代码重写为wpf,我迷路了

这就是我想做的:

这是我认为可能有效的东西,但它没有

<Grid ShowGridLines="True">
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
        <RowDefinition />
        <RowDefinition />
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
        <ColumnDefinition />
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>

    <ItemsControl ItemsSource="{Binding VirtualScreens}" Grid.IsSharedSizeScope="True" >
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Button Grid.Row="{Binding Row}" Grid.Column="{Binding Column}" Content="{Binding Name}"></Button>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Grid>

public class VirtualScreen : ObservableObject
{
    string name;
    int row;
    int column;

    public string Name
    {
        get { return name; }
        set
        {
            name = value;
            RaisePropertyChanged(() => Name);
        }
    }

    public int Row
    {
        get { return row; }
        set
        {
            row = value;
            RaisePropertyChanged(() => Row);
        }
    }

    public int Column
    {
        get { return column; }
        set
        {
            this.column = value;
            RaisePropertyChanged(() => Column);
        }
    }
}

公共类虚拟屏幕:可观察对象
{
字符串名;
int行;
int列;
公共字符串名
{
获取{返回名称;}
设置
{
名称=值;
RaisePropertyChanged(()=>名称);
}
}
公共int行
{
获取{返回行;}
设置
{
行=值;
RaisePropertyChanged(()=>行);
}
}
公共int列
{
获取{return column;}
设置
{
this.column=值;
RaisePropertyChanged(()=>列);
}
}
}
感谢您提供的任何类型的帮助

您可以使用来布局UI中显示的元素。三行三列的网格适合您:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition /> 
        <RowDefinition /> 
        <RowDefinition /> 
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
     <Button Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2" Grid.RowSpan="2">This is the big top left section</Button>
     <Button Grid.Row="1" Grid.Column="2">Top right</Button>
     <Button Grid.Row="2" Grid.Column="2">Middle right</Button>
     <Button Grid.Row="3" Grid.Column="1">Bottom left</Button>
     <Button Grid.Row="3" Grid.Column="2">Bottom center</Button>
     <Button Grid.Row="3" Grid.Column="2">Bottom right</Button>
</Grid>

这是左上角的大区域
右上角
中右翼
左下角
底部中心
右下角

查看更多信息。

您可以使用ItemsControl ItemsPanel、ItemsTemplate和ItemContainerStyle

这里有一个例子

<ItemsControl ItemsSource="{Binding VirtualScreens}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Grid ShowGridLines="True">
                <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition />
                    <ColumnDefinition />
                    <ColumnDefinition />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
            </Grid>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>

    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Name}" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>

    <ItemsControl.ItemContainerStyle>
        <Style>
            <Style.Setters>
                <Setter Property="Grid.Row" Value="{Binding Row}" />
                <Setter Property="Grid.Column" Value="{Binding Column}" />
            </Style.Setters>
        </Style>
    </ItemsControl.ItemContainerStyle>
</ItemsControl>


很抱歉,您需要提供更多详细信息,或者WinForms应用程序的屏幕截图,以便我们更好地了解您试图完成的任务。在StackOverflow中发布问题之前,请先阅读。网格可以这样布置,并随窗口自动调整内容大小。画布使用x、y、w、h坐标系来布局控件,但它们要求您在调整窗口大小时更新布局。在提问之前尝试创建一个小型原型,可以防止不必要的否决票。这是我想使用MVVM动态执行的操作。我不清楚你在问什么。“动态使用MVVM”-你的意思是你想用代码创建这个布局?非常感谢,这正是我想要的!!