[英]ItemsControl In Windows Phone 8.1 project
有必要在某些单元格中排列元素,但它们都属于左上单元格。 由于动态创建行和列,因此无法进行思考,但是在静态广告中,没有任何变化。
请帮忙。
XAML
<ItemsControl x:Name="ItControl" ItemTemplate="{StaticResource DefaultGridItemTemplate}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid >
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition/>
</Grid.ColumnDefinitions>
</Grid>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Grid.Column" Value="{Binding Col}"/>
<Setter Property="Grid.Row" Value="{Binding Row}"/>
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
C#
public class FilterItem
{
public int id_node { get; set; }
public int id_h { get; set; }
//[MaxLength(50)]
public string name { get; set; }
public int Col { get; set; }
public int Row { get; set; }
}
不幸的是,由于容器中没有XAML,因此没有任何可绑定的内容,因此无法使用。 您需要做的是将ItemsControl子类化并重写PrepareContainerForItemOverride函数,如下所示:
public class CustomItemsControl : ItemsControl
{
protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
{
base.PrepareContainerForItemOverride(element, item);
FrameworkElement source = element as FrameworkElement;
source.SetBinding(Grid.ColumnProperty, new Binding { Path = new PropertyPath("Col") });
source.SetBinding(Grid.RowProperty, new Binding { Path = new PropertyPath("Row") });
}
}
然后将XAML更改为CustomItemsControl而不是items控件:
<local:CustomItemsControl x:Name="ItControl" ItemTemplate="{StaticResource DefaultGridItemTemplate}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid >
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition/>
</Grid.ColumnDefinitions>
</Grid>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<!--<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Grid.Column" Value="{Binding Col}" />
<Setter Property="Grid.Row" Value="{Binding Row}"/>
</Style>
</ItemsControl.ItemContainerStyle>-->
</local:CustomItemsControl>
尝试在垂直方向的StackPanel中添加主ItemsControl。
谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.