我有ListView
,这对我有用。
<ListView x:Name="calendar" Grid.Column="1" Grid.Row="2"
AllowDrop="True"
SelectionMode="Single"
MouseMove="kal_MouseMove"
MouseUp="kal_MouseUp"
ItemsSource="{Binding CinnostiNaTyden}"
SelectedItem="{Binding SelectedCinnost}"
Datum="{Binding Datum}"
NasledujiciTydenCommand="{Binding NasledujiciTydenCommand}"
PredchoziTydenCommand="{Binding PredchoziTydenCommand}"
ItemAddedCommand="{Binding ItemAddedCommand}"
DragEnter="kal_DragEnter"
DragOver="kal_DragOver"
Drop="kal_Drop"
>
<ListView.InputBindings>
<KeyBinding Key="Delete" Command="{Binding DeleteCommand}" />
</ListView.InputBindings>
<ListView.ItemTemplate>
<DataTemplate>
<Grid Grid.Column="{Binding DatumCas, Converter={StaticResource ColumnConverter}}"
Grid.Row="{Binding DatumCas, Converter={StaticResource RowConverter}, Mode=TwoWay}"
Grid.RowSpan="{Binding Trvani, Converter={StaticResource RowSpanConverter}, Mode=TwoWay}"
MouseDown="Grid_MouseDown">
<TextBlock Text="{Binding Popis}"
Padding="5,2,0,0"
FontWeight="Bold"
TextWrapping="Wrap" />
<!--<Label Content="{Binding Trvani}" HorizontalAlignment="Center" VerticalAlignment="Center" />-->
<Label Cursor="SizeNS" Height="5" Background="#00000000" VerticalAlignment="Bottom" HorizontalAlignment="Stretch" MouseDown="LabelSpodni_MouseDown" Margin="0,0,0,-2"/>
<Label Cursor="SizeNS" Height="5" Background="#00000000" VerticalAlignment="Top" HorizontalAlignment="Stretch" MouseDown="LabelHorni_MouseDown" Margin="0,-2,20,0"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.Resources>
<Style TargetType="{x:Type ListViewItem}">
<!--<Setter Property="FocusVisualStyle" Value="{x:Null}"/>-->
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListViewItem}">
<Border CornerRadius="5"
BorderThickness="2"
BorderBrush="DodgerBlue"
Background="#CC4f9dea"
FocusVisualStyle="{x:Null}"
Margin="0,0,22,2"
x:Name="Bd"
Padding="0,0,0,0"
SnapsToDevicePixels="true">
<Grid>
<ContentPresenter HorizontalAlignment="Stretch" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Stretch" />
<Button x:Name="btnClose"
Opacity="0"
Content="X"
Command="{Binding DeleteCommand}"
HorizontalAlignment="Right" VerticalAlignment="Top"
Padding="0,0,0,0"
HorizontalContentAlignment="Center" VerticalContentAlignment="Center"
Height="20" Width="20"
Style="{StaticResource RoundedButtonStyle}"
FontWeight="Bold"
FontFamily="Consolas"
Cursor="Hand"/>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" TargetName="Bd" Value="#CC4f9dea" />
<Setter Property="Tag" TargetName="btnClose" Value="Visible" />
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="Selector.IsSelectionActive" Value="True" />
<Condition Property="IsSelected" Value="True" />
</MultiTrigger.Conditions>
<Setter Property="Background" TargetName="Bd" Value="#cc96c0ea" />
</MultiTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListView.Resources>
</ListView>
现在看:
首先我像这样设置ListView.ItemTemplate
:
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
...
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
然后我设置了这个:
<ListView.Resources>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListViewItem}">
<Border CornerRadius="5" Name="Bd">
<Grid>
<ContentPresenter/>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" TargetName="Bd" Value="#CC4f9dea" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListView.Resources>
首先,我设置ListView.ItemTemplate
,然后为Template
的{{1}}设置TargetType
。首先是ListViewItem
,第二个是DataTemplate
。但ControlTemplate
和ListView.ItemTemplate
之间有什么区别?
答案 0 :(得分:1)
但
ListView.ItemTemplate
和ListViewItem.Template
之间有什么区别?
ItemTemplate
是DataTemplate
,适用于ItemsSource
的{{1}}中的每个项目。 ListView
的根元素在运行时隐式包装在ItemTemplate
容器中,并且此容器具有默认样式,默认样式为ListViewItem
,用于定义容器的默认外观。
通过定义设置容器的ControlTemplate
属性的ListViewItem
样式,您可以使用自己的自定义模板“覆盖”此默认模板。这就是为什么您在使用问题中的最后一个自定义Template
选择项目时不会获得任何突出显示的原因。