Pseudo-Infinite Grid
I've got a bit of a design issue here.
I've got a view:
<ItemsControl x:Nam开发者_如何学Goe="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/
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)
精彩评论