[英]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.