我遇到一个奇怪的问题…
我想我要做的是很标准的:允许用户在Grid之间切换和图标模式。一切都很顺利,但是……Icon视图不是在换行中显示项目,而是在单个列中显示项目,每个项目占据视图的整个宽度。我也说不清到底是哪里出了问题……: - (
(我还没有在这个论坛上获得足够的经验,它不允许我发布图片;我将给出屏幕截图的链接)
我想要的:https://i.stack.imgur.com/jYhVx.png
我有什么:https://i.stack.imgur.com/PeAae.png
以下是IconView样式定义(在ThemesGeneric.xaml中):
<Style x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type l:IconView}, ResourceId=IconViewStyle}"
TargetType="{x:Type ListView}"
BasedOn="{StaticResource {x:Type ListBox}}">
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="ItemContainerStyle" Value="{Binding (ListView.View).ItemContainerStyle, RelativeSource={RelativeSource Self}}"/>
<Setter Property="ItemTemplate" Value="{Binding (ListView.View).ItemTemplate, RelativeSource={RelativeSource Self}}"/>
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<WrapPanel IsItemsHost="True"
Width="{Binding (FrameworkElement.ActualWidth), RelativeSource={RelativeSource AncestorType=ScrollContentPresenter}}"
ItemWidth="{Binding (ListView.View).ItemWidth, RelativeSource={RelativeSource AncestorType=ListView}}"
MinWidth="{Binding ItemWidth, RelativeSource={RelativeSource Self}}"
ItemHeight="{Binding (ListView.View).ItemHeight, RelativeSource={RelativeSource AncestorType=ListView}}"/>
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
</Style>
在相应的Control类中使用:
public class IconView : ViewBase
{
public static readonly DependencyProperty ItemContainerStyleProperty =
ItemsControl.ItemContainerStyleProperty.AddOwner(typeof(IconView));
public Style ItemContainerStyle
{
get { return (Style)GetValue(ItemContainerStyleProperty); }
set { SetValue(ItemContainerStyleProperty, value); }
}
public static readonly DependencyProperty ItemTemplateProperty =
ItemsControl.ItemTemplateProperty.AddOwner(typeof(IconView));
public DataTemplate ItemTemplate
{
get { return (DataTemplate)GetValue(ItemTemplateProperty); }
set { SetValue(ItemTemplateProperty, value); }
}
public static readonly DependencyProperty ItemWidthProperty =
WrapPanel.ItemWidthProperty.AddOwner(typeof(IconView));
public double ItemWidth
{
get { return (double)GetValue(ItemWidthProperty); }
set { SetValue(ItemWidthProperty, value); }
}
public static readonly DependencyProperty ItemHeightProperty =
WrapPanel.ItemHeightProperty.AddOwner(typeof(IconView));
public double ItemHeight
{
get { return (double)GetValue(ItemHeightProperty); }
set { SetValue(ItemHeightProperty, value); }
}
protected override object DefaultStyleKey
{
get
{
return new ComponentResourceKey(GetType(), "IconViewStyle");
}
}
}
这些在视图中是如何被使用的。xaml(为了简洁起见,我将省略为ListView的View分配{DynamicResource IconView}的DataTrigger):
<DataTemplate x:Key="IconViewItemTemplate">
<StackPanel Height="170" Width="170">
<Grid Width="150" Height="150" HorizontalAlignment="Center">
<Image Source="{Binding DefaultPicture.Path}" Margin="6,6,6,9"/>
</Grid>
<TextBlock Text="{Binding ID}" FontSize="13" HorizontalAlignment="Center" Margin="0,0,0,1" />
</StackPanel>
</DataTemplate>
<localControls:IconView x:Key="IconView"
ItemTemplate="{StaticResource IconViewItemTemplate}"
ItemWidth="180"/>
我要疯了…更让我沮丧的是,Snoop看不到我的应用程序:-(
)请帮忙!: -)
很多谢谢,亚历克斯
您的大多数绑定可能只是被破坏:(ListView.View).ItemWidth
上述路径的解释方式与StoryBoard.TargetProperty
中使用的路径不同。如果在绑定中使用括号,则表示绑定到附加属性。
来自MSDN,强调我的:
路径在XAML中指定,该路径属于没有指定Target Type的样式或模板。合格的用法通常对以外的情况无效,因为在非样式、非模板的情况下,属性存在于实例上,而不是类型上。
因此分别更改它们,在上面的示例中:View.ItemWidth
我找到了罪魁祸首。事实证明,问题不在于我在问题中包含的片段,而在于我遗漏的东西- ListView。ListView本身的GroupStyle定义。删除它后,列表显示为我所期望的方式。
感谢每个考虑我问题的人!
亚历克斯