我在XAML中创建了一个嵌套网格,目前看起来像这样:
<Grid x:Name="mainGrid" >
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid Grid.Row="{Binding Row}" Grid.Column="{Binding Col}">
<Grid.RowDefinitions>
<RowDefinition Height="0.1*"/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Text="{Binding Title}" VerticalAlignment="Bottom" HorizontalAlignment="Center" Grid.Row="0" FontWeight="Bold" TextDecorations="Underline"/>
<ListView ItemsSource="{Binding Names}" Grid.Row="1"/>
</Grid>
</Grid>
我的目标是填充内部Grids
模板后面的4 Grid
。我通过使用以下代码尝试了这一点:
List<Test> tests = new List<Test>();
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
Test t = new Test();
t.Title = i + "\t" + j;
t.Row = i;
t.Col = j;
tests.Add(t);
}
}
mainGrid.DataContext = tests;
和班级定义:
class Test
{
public string Title { get; set; }
public int Row { get; set; }
public int Col { get; set; }
public ObservableCollection<string> Names
{
get
{
return new ObservableCollection<string>() { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J"};
}
}
}
我的想法是,对于外Grid
定义的每个段,我有4个具有行/列定义的对象(0,0&amp; 0,1&amp; 1,0&amp; 1,1 )。内部Grid
将作为ItemTemplate
中ListView
的模板。但是只填充了顶部(0,0)Grid
。
我应该采取哪些步骤,以便内部Grid
的行为类似于模板,并且所有4个Grids
都已创建并填充?
答案 0 :(得分:4)
使用ItemsControl
,其中ItemsSource
是Test
的列表,并使用内ItemsPanel
和Grid
填充ItemTemplate
内Grid
1}}
<ItemsControl ItemsSource="{Binding}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
</Grid>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="{x:Type ContentPresenter}">
<Setter Property="Grid.Row" Value="{Binding Row}"/>
<Setter Property="Grid.Column" Value="{Binding Col}"/>
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="0.1*"/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Text="{Binding Title}" VerticalAlignment="Bottom" HorizontalAlignment="Center" Grid.Row="0" FontWeight="Bold" TextDecorations="Underline"/>
<ListView ItemsSource="{Binding Names}" Grid.Row="1"/>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
因为ItemsControl
将在ContentPresenter
中换行项目,您需要将Grid.Column
和Grid.Row
绑定移至ItemContainerStyle
修改
ItemsControl
是显示某些内容列表的最基本控件。像ListBox
,ListView
,ComboBox
,DataGrid
甚至MenuItem
这样的控件都是从ItemsControl
继承的,可以在顶部添加更多功能,例如选择或列视图。
ItemsPanel
定义了常规项容器的外观,ItemTemplate
定义了集合中每个项目的外观。
每个ItemsControl
都有其商品容器类型。 ListBox
为ListBoxItem
,ListView
为ListViewItem
,依此类推,ItemsControl
为ContentPresenter
。它只是一个可视容器,其中列表的每个项目都作为内容放置,然后 - 根据层次结构MSDN: Remarks section - 转换为可视化。在可视化树中,此容器是项目面板的直接子项,因此必须对其设置事项列表Grid.Column
和/或Grid.Row
。另外,例如ListBox
ListBoxItem
持有IsSelected
属性的ItemContainerStyle
。更改容器或获取容器数据的方法是<?php
class Devinc_Ajaxcart_Block_Product_List_New extendsMage_Catalog_Block_Product_New
{
protected function _construct()
{
$this->_productsCount = Mage::getStoreConfig('ajaxcart/popup_configuration/product_number');
parent::_construct();
}
public function getAdditionalContentType() {
return Mage::getStoreConfig('ajaxcart/popup_configuration/additional_content');
}
public function getAdditionalContent() {
return $this->getProductCollection();
}
public function getAcAddToCartUrl($_product) {
$params = Mage::helper('ajaxcart')->getUrlParams($this->getAddToCartUrl($_product));
unset($params['uenc']);
$url = Mage::getUrl('ajaxcart/index/init', $params);
return "ajaxcart.initAjaxcart('".$url."', this, 'success');";
}
public function getAcAddToWishlistUrl($_product) {
$url = str_replace('wishlist/index/add/','ajaxcart/wishlist/add/', $this->helper('wishlist')->getAddUrl($_product));
if (Mage::helper('customer')->isLoggedIn()) {
return "javascript:ajaxcart.addToWishlist('".$url."', 'success')";
} else {
return "javascript:ajaxcartLogin.loadLoginPopup('".$url."', 'success');";
}
}
public function getAcAddToCompareUrl($_compareUrl) {
$url = str_replace('catalog/product_compare/add', 'ajaxcart/product_compare/add', $_compareUrl);
return "javascript:ajaxcart.addToCompare('".$url."', 'success')";
}
。