C# WP8中ItemPanelTemplate上的VariableSizedWrapGrid
在WP8上,我希望在如下页面上显示列表: 我知道使用WP8.1和VariableSizedWrapGrid控件可以很容易地做到这一点。但是我需要让它在WP8.0上工作。所以我尝试了Kinnara的工具包: 其中包含一个VariableSizedWrapGrid 我可以做我想要的显示(参见屏幕截图),但我不能在长列表选择器或列表框中使用它作为ItemPanelTemplate 以下是我尝试过的代码(它的灵感来自于一种在Windows 8上工作的方法,但不幸的是,它似乎在WP8上不起作用)以及以下博文: 现在在后面的代码中,我只是用一个项目列表(具有ItemSize属性)填充LB_Wings: 不幸的是,我在初始化页面时遇到错误:“无法创建SpecificListBox类型的实例” 我想我已经接近解决办法了。有谁能帮我用一个列表框和一个ItemPanelTemplate来做这种演示(在WP8上似乎很自然)C# WP8中ItemPanelTemplate上的VariableSizedWrapGrid,c#,xaml,windows-phone-7,windows-phone-8,variablesizedwrapgrid,C#,Xaml,Windows Phone 7,Windows Phone 8,Variablesizedwrapgrid,在WP8上,我希望在如下页面上显示列表: 我知道使用WP8.1和VariableSizedWrapGrid控件可以很容易地做到这一点。但是我需要让它在WP8.0上工作。所以我尝试了Kinnara的工具包: 其中包含一个VariableSizedWrapGrid 我可以做我想要的显示(参见屏幕截图),但我不能在长列表选择器或列表框中使用它作为ItemPanelTemplate 以下是我尝试过的代码(它的灵感来自于一种在Windows 8上工作的方法,但不幸的是,它似乎在WP8上不起作用)以及以下
非常感谢您所要做的就是将SpecificListBox公开。类: 编辑 此外,您不需要SpecificListBox的ItemContainerStyle中的绑定。我用Horizontal/VerticalContentAlignment=Stretch更改了这些,因为我觉得这样看起来更好。如果没有此项,DataTemplate中的控件就不会占据该项的所有位置。以下是更改后的xaml:
<local:SpecificListBox x:Name="LB_Wings" ItemTemplate="{StaticResource IT_Wings}" >
<local:SpecificListBox.ItemsPanel>
<ItemsPanelTemplate>
<toolkit:VariableSizedWrapGrid ItemWidth="100" ItemHeight="100" MaximumRowsOrColumns="4"></toolkit:VariableSizedWrapGrid>
</ItemsPanelTemplate>
</local:SpecificListBox.ItemsPanel>
<local:SpecificListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="VerticalContentAlignment" Value="Stretch" />
</Style>
</local:SpecificListBox.ItemContainerStyle>
</local:SpecificListBox>
谢谢,但我在解析时遇到另一个错误:“未能分配到此行的属性'System.Windows.Setter.Value'”:我无法从工具箱中找到将我的itemSize绑定到此控件的属性的方法……好吧,您不需要该绑定。如果使用SpecificListBox,将在PrepareContainerForItemOverride方法中设置要绑定的值。如果你想把它们绑定起来,你可以用SetBinding代替SetValue。我不明白。你能说得更具体些吗?我在PrepareContainerForItemOverride方法中做错了什么?谢谢你的帮助我把答案修改了一点。用这种方法一切都好。问题出在ItemContainerStyle中。我不认为可以在Setter值中使用绑定。我尝试了这个,我没有任何错误,但是我的所有项目都以相同的大小显示。我不讨论PrepareContainerForItemOverride方法。调用该方法需要做什么?
<DataTemplate x:Key="IT_Wings">
<Grid Margin="0,0,12,12" >
<Image Source="{Binding BI_List}" Stretch="UniformToFill" </Image>
</Grid>
</DataTemplate>
class SpecificListBox : ListBox
{
public SpecificListBox() { }
protected override void PrepareContainerForItemOverride(System.Windows.DependencyObject element, object item)
{
dynamic lateBoundItem = item;
int sizeFactor = (int)lateBoundItem.ItemSize;
element.SetValue(VariableSizedWrapGrid.ColumnSpanProperty, sizeFactor);
element.SetValue(VariableSizedWrapGrid.RowSpanProperty, sizeFactor);
base.PrepareContainerForItemOverride(element, item);
}
}
LB_Wings.ItemsSource = listOfItems; //a list of items that have the ItemSize property.
public class SpecificListBox : ListBox
<local:SpecificListBox x:Name="LB_Wings" ItemTemplate="{StaticResource IT_Wings}" >
<local:SpecificListBox.ItemsPanel>
<ItemsPanelTemplate>
<toolkit:VariableSizedWrapGrid ItemWidth="100" ItemHeight="100" MaximumRowsOrColumns="4"></toolkit:VariableSizedWrapGrid>
</ItemsPanelTemplate>
</local:SpecificListBox.ItemsPanel>
<local:SpecificListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="VerticalContentAlignment" Value="Stretch" />
</Style>
</local:SpecificListBox.ItemContainerStyle>
</local:SpecificListBox>