WPF. How do I create many LIstViews with the same look
I've defined a listview in my recent project and realized that I will be using more listviews looking exactly the same and having the same solumns. Since I'm new to WPF am I curious of teh best way to do this. Is it to create a usercontrol? Use styles? I've tried to use styles but it didnt work the way I was hoping. I tried to set the "View" property using style, like this.
<Style x:Key="ListViewStyle" TargetType="{x:Type ListView}">
<Setter Property="View">
<ListV开发者_Go百科iew.View>
<GridView>
But it didnt work so I'm asking for your opinion?! Thanks.
You're missing the Setter.Value
tag, and you don't need the ListView.View
tag :
<Style x:Key="ListViewStyle" TargetType="{x:Type ListView}">
<Setter Property="View">
<Setter.Value>
<GridView>
Yes you should go ahead and put the common properties into a Style and link to it from all the similar controls...
The issue I see from your brief code-snippet is that you don't need the ListView.List element (the A.B XAML Syntax means that you're setting the B property on A.)
<Style ...
<Setter Property="PropertyName">
<ValueObj/>
</Setter>
<Setter Property="View">
<GridView ... />
</Setter>
</Style>
Another possibility that hasn't been discussed: you can define the ListView
in a DataTemplate
, and use that DataTemplate
to display the various collections you'll be showing. For instance, if your items collections are all of type MyCollectionType
, you can do this:
<DataTemplate DataType="myNamespace:MyCollectionType">
<ListView ItemsSource="{Binding Items}">
<!-- define everything here -->
</ListView>
</DataTemplate>
...
<StackPanel>
<ContentControl Content="{Binding OneCollection}"/>
<ContentControl Content="{Binding AnotherCollection"/>
</StackPanel>
and the OneCollection
and AnotherCollection
properties in your view model will both be rendered as ListView
controls with the same layout.
精彩评论