wpf 如何使 ListViewItem 的模板样式和 ListView.ItemTemplate 都能工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30251604/
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
How to make both ListViewItem's template style and ListView.ItemTemplate working?
提问by user3819226
I have the code below in my App.xaml. I override the template to make the background stays the same as transparent when I click/hover on a ListViewItem.
我的 App.xaml 中有以下代码。当我在 ListViewItem 上单击/悬停时,我会覆盖模板以使背景保持透明。
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="Background" Value="Transparent" />
<Setter Property="Height" Value="{StaticResource RowHeight}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListViewItem}">
<Border
BorderBrush="Transparent"
BorderThickness="0"
Background="{TemplateBinding Background}">
<GridViewRowPresenter HorizontalAlignment="Stretch" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Width="Auto" Margin="0" Content="{TemplateBinding Content}"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
And I asked a question yesterday How to add a underlying progressbar across two or more ListViewItem in wpf?So I wrote below and it works greatly if I didn't apply the style above. If I do so, I will see nothing in the ListView.
我昨天问了一个问题 如何在 wpf 中跨两个或多个 ListViewItem 添加底层进度条?所以我写在下面,如果我不应用上面的样式,它会很有用。如果我这样做,我将在 ListView 中看不到任何内容。
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<ProgressBar Grid.ColumnSpan="2" Grid.ZIndex="0" Value="{Binding ProgressBarValue}"/>
<Label Grid.Column="0" Grid.ZIndex="1" Content="{Binding CCC}"/>
<Label Grid.Column="1" Grid.ZIndex="1" Content="{Binding DDD}"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
I do need to make the background transparent but why won't these codes work together? How can I improve?
我确实需要使背景透明,但为什么这些代码不能一起工作?我该如何改进?
In addition, how do you guys know what's wrong here? I have no idea where can I find reference/explanations or to study the mechanism behind xaml code..
另外,你们怎么知道这里出了什么问题?我不知道在哪里可以找到参考/解释或研究 xaml 代码背后的机制..
回答by Dinesh balan
Try Like this:
试试这样:
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListViewItem}">
<Border
BorderBrush="Transparent"`enter code here`
BorderThickness="0"
Background="{TemplateBinding Background}">
<GridViewRowPresenter HorizontalAlignment="Stretch" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Width="Auto" Margin="0" Content="{TemplateBinding Content}"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate>
<Grid Background="Red">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<ProgressBar Grid.ColumnSpan="2" Grid.ZIndex="0" Value="30"/>
<Label Grid.Column="0" Grid.ZIndex="1" Content="{binding}"/>
<Label Grid.Column="1" Grid.ZIndex="1" Text="{binding}"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
And I am using the book WPF 4.5 Unleashedas reference for Xaml.
我使用WPF 4.5 Unleashed这本书作为 Xaml 的参考。

