WPF:如何为ListView创建自定义视图模式

时间:2018-10-30 11:48:21

标签: c# wpf listview

我试图为here描述的ListView实现自定义视图模式,但未成功:未应用视图的WrapPanelItemTemplate。而且没有完整的代码可用,因此我不知道如何创建listview对象。这是上面示例中的完整代码:

   <Window.Resources>
    <Style x:Key="{ComponentResourceKey 
  TypeInTargetAssembly={x:Type local:PlainView},
  ResourceId=myPlainViewDSK}" 
   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 Width="{Binding (FrameworkElement.ActualWidth),
               RelativeSource={RelativeSource 
                               AncestorType=ScrollContentPresenter}}"
               ItemWidth="{Binding (ListView.View).ItemWidth,
               RelativeSource={RelativeSource AncestorType=ListView}}"
               MinWidth="{Binding (ListView.View).ItemWidth,
               RelativeSource={RelativeSource AncestorType=ListView}}"
               ItemHeight="{Binding (ListView.View).ItemHeight,
               RelativeSource={RelativeSource AncestorType=ListView}}"/>
                </ItemsPanelTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <DataTemplate x:Key="centralTile">
        <StackPanel Height="100" Width="90">                
            <TextBlock Text="{Binding}" FontSize="13" 
           HorizontalAlignment="Center" Margin="0,0,0,1" />
            <TextBlock Text="{Binding}" FontSize="9" 
           HorizontalAlignment="Center" Margin="0,0,0,1" />
        </StackPanel>
    </DataTemplate>

    <local:PlainView x:Key="tileView" 
         ItemTemplate="{StaticResource centralTile}" 
         ItemWidth="100"/>
</Window.Resources>

<Grid>
    <ListView x:Name="lv" ItemsSource="{Binding Entries}" />
</Grid>

.cs

    public class ViewModel
{
    public ObservableCollection<string> Entries { get; private set; }

    public ViewModel()
    {
        Entries = new ObservableCollection<string>();
        Entries.Add("one");
        Entries.Add("two");
        Entries.Add("three");
        Entries.Add("four");
    }
}
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        DataContext = new ViewModel();
        lv.View = lv.FindResource("tileView") as ViewBase;
    }
}

我缺少什么?

1 个答案:

答案 0 :(得分:0)

您必须为ListView定义样式。我也很难弄清楚如何使用ComponentResourceKey作为样式的Key。但最后我明白了,请参阅下面的ListView。

<Grid>
    <ListView x:Name="lv" ItemsSource="{Binding Entries}"  Style="{StaticResource {ComponentResourceKey TypeInTargetAssembly={x:Type local:PlainView},ResourceId=myPlainViewDSK}}" />
</Grid>