wpf 将样式从资源应用到 ListView.ItemContainerStyle
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21678704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Apply Style From Resource to ListView.ItemContainerStyle
提问by HK1
I'm working with XAML/WPF in VS 2012. I'll admit I don't really understand templating and styling very well yet.
我正在 VS 2012 中使用 XAML/WPF。我承认我还不太了解模板和样式。
I've defined a style in my application.xaml file like this:
我在 application.xaml 文件中定义了一个样式,如下所示:
<Style x:Key="ContactGroups" TargetType="ListViewItem">
<!-- Styling omitted here -->
</Style>
Now I want to apply this style to my list view but I cannot figure out where to apply this styling, i.e., where to put the code to set the style. I've omitted lots of attributes here to keep things shorter:
现在我想将此样式应用于我的列表视图,但我无法弄清楚在哪里应用此样式,即,将代码放置在哪里来设置样式。我在这里省略了很多属性以保持简短:
<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>
回答by Rohit Vats
Use StaticResourcemarkup extension to set Style on ItemContainerStyleof ListBox:
使用StaticResource标记扩展来设置ItemContainerStyleListBox 的样式:
<ListView ItemsSource="{Binding Groups}"
SelectedItem="{Binding Path=SelectedGroup, Mode=OneWayToSource}"
ItemContainerStyle="{StaticResource ContactGroups}" >
回答by TrueEddie
I'm not sure if you only want this style to be applied to this list, but if not you could simply remove the x:Key="ContactGroups"from the style and it should be applied to all list items.
我不确定您是否只想将此样式应用于此列表,但如果不是,您可以简单地x:Key="ContactGroups"从样式中删除,它应该应用于所有列表项。
If you are looking to only target this listview an option would be to add the style to the resources of the list view:
如果您只想定位此列表视图,则可以选择将样式添加到列表视图的资源中:
<ListView ItemsSource="{Binding Groups}" SelectedItem="{Binding Path=SelectedGroup, Mode=OneWayToSource}" >
<ListView.Resources>
<Style TargetType="ListViewItem">
<!-- Styling omitted here -->
</Style>
</ListView.Resources>
<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>
Edit: Based on your comment below this may be the approach you want to take:
编辑:根据您在下面的评论,这可能是您想要采取的方法:
<ListView.Resources>
<Style TargetType="ListViewItem" BasedOn="{StaticResource ContactGroups}" />
</ListView.Resources>
This way your style stays defined in the App.xaml.
这样,您的样式将在 App.xaml 中保持定义。

