我在VS 2012中使用XAML/WPF.我承认我还没有真正理解模板和样式.
我在application.xaml文件中定义了一个样式,如下所示:
<Style x:Key="ContactGroups" TargetType="ListViewItem">
<!-- Styling omitted here -->
</Style>
Run Code Online (Sandbox Code Playgroud)
现在我想将这个样式应用到我的列表视图中,但我无法弄清楚应用这个样式的位置,即在哪里放置代码来设置样式.我在这里省略了很多属性来缩短时间:
<ListView ItemsSource="{Binding Groups}" SelectedItem="{Binding Path=SelectedGroup, Mode=OneWayToSource}" >
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemTemplate>
<DataTemplate>
<Grid Width="140" Height="25">
<Grid.RowDefinitions>
<RowDefinition Height="2*" />
</Grid.RowDefinitions>
<Label Content="{Binding Name}" ToolTip="{Binding Name}" />
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Run Code Online (Sandbox Code Playgroud)
使用StaticResource标记扩展来设置ItemContainerStyle
ListBox的Style :
<ListView ItemsSource="{Binding Groups}"
SelectedItem="{Binding Path=SelectedGroup, Mode=OneWayToSource}"
ItemContainerStyle="{StaticResource ContactGroups}" >
Run Code Online (Sandbox Code Playgroud)