WPF ListView:图标视图显示在一列中

WPF ListView:图标视图显示在一列中,wpf,listview,listviewitem,Wpf,Listview,Listviewitem,我遇到了一个奇怪的问题 我想我尝试做的是相当标准的:允许用户在网格之间切换 和图标模式在我的ListView中。 一切都很顺利,但是。。。图标视图不是以换行的方式显示项目,而是将项目显示在一列中,每个项目占据视图的整个宽度。我不能确定到底是什么错了…:-( (我在这个论坛上还没有获得足够的经验,而且它不允许我发布图片;我将提供屏幕截图的链接) 我想要的是: 我所拥有的: 以下是IconView样式定义(在Themes\Generic.xaml中): 下面是如何在View.xaml中使用所有这些(

我遇到了一个奇怪的问题

我想我尝试做的是相当标准的:允许用户在网格之间切换 和图标模式在我的ListView中。 一切都很顺利,但是。。。图标视图不是以换行的方式显示项目,而是将项目显示在一列中,每个项目占据视图的整个宽度。我不能确定到底是什么错了…:-(

(我在这个论坛上还没有获得足够的经验,而且它不允许我发布图片;我将提供屏幕截图的链接)

我想要的是:

我所拥有的:

以下是IconView样式定义(在Themes\Generic.xaml中):

下面是如何在View.xaml中使用所有这些(为了简洁起见,我将省略将{DynamicResource IconView}分配给ListView视图的DataTrigger):


我快发疯了……雪上加霜的是,Snoop没有看到我的应用程序:-(

请帮忙!;-)

非常感谢,,
Alex

您的大多数绑定可能都已损坏:
(ListView.View).ItemWidth

上述路径的解释与您在故事板中使用的路径不同。例如,TargetProperty。如果在绑定中使用括号,则表示绑定到附加属性

我的重点是:

路径是在XAML中指定的,该XAML的样式或模板没有指定的目标类型。限定用法通常对除此之外的情况无效,因为在非样式、非模板的情况下,属性存在于实例上,而不是类型上

因此,在上面的示例中分别更改它们:
View.ItemWidth

好吧,我找到了罪魁祸首。 事实证明,问题不在于我在问题中包含的代码片段,而在于我遗漏的东西——ListView本身的ListView.GroupStyle定义。 删除后,列表将按我期望的方式显示

谢谢所有考虑过我问题的人


Alex

关于snoop…如果您正在运行VS提升(在管理模式下),您还需要运行提升版的snoop。此外,除非您有更高版本的snoop,否则您需要确保您的snoop版本与应用程序的32/64位匹配。Dan,我已经尝试以管理员身份运行snoop,然后我的应用程序和snoop…没有任何帮助。是的,我刚刚从下载了最新版本CodePlex今天。尝试了一下,似乎没有改变任何东西…:-(@Alex:你检查了绑定是否工作了吗?例如,输出窗口中是否有绑定错误?
<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>
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");
        }
    }
}
<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"/>