伪无限网格

发布于 2024-12-01 22:31:59 字数 1515 浏览 0 评论 0原文

我在这里遇到了一些设计问题。

我有一个视图:

<ItemsControl x:Name="CellVMs">
    <ItemsControl.ItemsPanel>
      <ItemsPanelTemplate>
            <Grid />
         </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemContainerStyle>
      <Style>
        <Setter Property="Grid.Row"
                Value="{Binding Position.Y}" />
        <Setter Property="Grid.Column"
                Value="{Binding Position.X}" />
      </Style>
    </ItemsControl.ItemContainerStyle>
  </ItemsControl>

它绑定到视图模型的集合,该集合具有一个位置属性,其中的样式使用该属性将其定位在 ItemPanelTemplate 上。 (每个单元格只有一个视图模型,并且网格单元格大小固定)

1)我希望网格是伪无限的,即随着 EditorVM 的添加和减少,网格应该动态添加和删除 Row\Col 定义,并且有应该始终有足够的网格,并且应该始终有足够的空间来填充父视图。

2)在我的包含视图模型中,我导入一个具有 Grid 属性的 IGridEditor 实现实例。如何将 ItemsPanelTemplateGrid 绑定到 IEditor.Grid?

现在,我将 CellVM 添加到 IGridEditor 方法中的集合,然后当包含虚拟机导入实例时,将包含虚拟机的 CellVM 集合设置为实例集合,并且项目控件使用 Caliburn.Micro 约定绑定到该集合。

顺便说一句,我正在使用 Caliburn.Micro\MEF。

谁能帮我解决这个问题吗?

编辑:

一直试图理解这一点,但我一无所获。

我唯一能找到的是

<ItemsControl.ItemsPanel>
         <ItemsPanelTemplate>
            <Grid x:Name="EditorGrid"/>
         </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>

在我的 viemodel 中:

[Import("EditorGrid", typeof(Grid))]
public Grid EditorGrid { get; set; }

以及类中相应的 Export,它具有将内容添加到网格的方法/

I've got a bit of a design issue here.

I've got a view:

<ItemsControl x:Name="CellVMs">
    <ItemsControl.ItemsPanel>
      <ItemsPanelTemplate>
            <Grid />
         </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemContainerStyle>
      <Style>
        <Setter Property="Grid.Row"
                Value="{Binding Position.Y}" />
        <Setter Property="Grid.Column"
                Value="{Binding Position.X}" />
      </Style>
    </ItemsControl.ItemContainerStyle>
  </ItemsControl>

which is bound to a collection of viewmodels, that have a position property which the style there uses to position it on the ItemPanelTemplate. (Only one viewmodel per cell, and the grid cells are fixed size)

1) I would like that Grid to be pseudo-infinite, ie as EditorVMs get added and subtracted, the Grid should dynamically add and delete Row\Col Definitions, and there should always be enough Grid, and there should always be enough to fill the parent view.

2) In my containing viewmodel, I import an IGridEditor implementation instance which has a Grid property. How can I bind the ItemsPanelTemplateGrid to the IEditor.Grid?

Right now, I add the CellVM's to a collection in the IGridEditor's methods, then when the containing vm imports the instance, sets the containing vm's CellVM collection to the instances collection, and the item control binds to that using Caliburn.Micro's conventions.

I'm using Caliburn.Micro\MEF btw.

Can anyone help me figure this out?

Edit:

Been trying to understand this, but I'm coming up empty.

Only thing I can find is

<ItemsControl.ItemsPanel>
         <ItemsPanelTemplate>
            <Grid x:Name="EditorGrid"/>
         </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>

and in my viemodel:

[Import("EditorGrid", typeof(Grid))]
public Grid EditorGrid { get; set; }

and a corresponding Export in class that has the methods to add things to the grid/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吻泪 2024-12-08 22:31:59

1) 我希望网格是伪无限的,即 EditorVMs 得到
添加和删​​除,Grid应该动态添加和删除
Row\Col 定义,并且应该始终有足够的 Grid,并且
应始终足以填充父视图。

您可以创建一个自定义Grid,根据需要自动添加行或列:(

public class AutoExpandGrid : Grid
{
    protected override System.Windows.Media.Visual GetVisualChild(int index)
    {
        var visualChild = base.GetVisualChild(index);

        var uiElement = visualChild as UIElement;
        if (uiElement != null)
            EnsureEnoughRowsAndColumns(uiElement);

        return visualChild;
    }

    private void EnsureEnoughRowsAndColumns(UIElement child)
    {
        int minRows = GetRow(child) + GetRowSpan(child);
        int minColumns = GetColumn(child) + GetColumnSpan(child);

        while (minRows > RowDefinitions.Count)
        {
            RowDefinitions.Add(new RowDefinition());
        }

        while (minColumns > ColumnDefinitions.Count)
        {
            ColumnDefinitions.Add(new ColumnDefinition());
        }
    }
}

不确定GetVisualChild是执行此操作的最佳位置,但这是我能找到的最好的位置)

1) I would like that Grid to be pseudo-infinite, ie as EditorVMs get
added and subtracted, the Grid should dynamically add and delete
Row\Col Definitions, and there should always be enough Grid, and there
should always be enough to fill the parent view.

You could create a custom Grid that automatically adds rows or columns as needed:

public class AutoExpandGrid : Grid
{
    protected override System.Windows.Media.Visual GetVisualChild(int index)
    {
        var visualChild = base.GetVisualChild(index);

        var uiElement = visualChild as UIElement;
        if (uiElement != null)
            EnsureEnoughRowsAndColumns(uiElement);

        return visualChild;
    }

    private void EnsureEnoughRowsAndColumns(UIElement child)
    {
        int minRows = GetRow(child) + GetRowSpan(child);
        int minColumns = GetColumn(child) + GetColumnSpan(child);

        while (minRows > RowDefinitions.Count)
        {
            RowDefinitions.Add(new RowDefinition());
        }

        while (minColumns > ColumnDefinitions.Count)
        {
            ColumnDefinitions.Add(new ColumnDefinition());
        }
    }
}

(not sure GetVisualChild is the best place to do this, but it's the best I could find)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文