DataTemplate中的IsSelected样式,触发器不适用
本文关键字:触发器 不适用 样式 中的 IsSelected DataTemplate | 更新日期: 2023-09-27 18:27:28
请找到以下代码。我使用了项目控制和数据模板。在数据模板内部,我有我需要的网格和边界。我已经使用datatemplate.Triggers为数据模板编写了样式。样式应用于mouseover,但样式不应用于Isselected。如何实现精选风格
Xaml:
<ItemsControl HorizontalAlignment="Center" Name="lvDataBinding">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid Margin="10,0,5,0" Width="500">
<Grid.RowDefinitions>
<RowDefinition Height="120*"/>
<RowDefinition Height="5"/>
<RowDefinition Height="120*"/>
<RowDefinition Height="5"/>
<RowDefinition Height="120*"/>
<RowDefinition Height="5"/>
<RowDefinition Height="120*"/>
</Grid.RowDefinitions>
<Border x:Name="FirstSelectionBorder" BorderBrush="#FF0087AA" >
<Border x:Name="FirstNormalBorder" BorderBrush="#FF555555" BorderThickness="1">
<Grid x:Name="FirstGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="10"/>
<ColumnDefinition Width="300*"/>
<ColumnDefinition Width="5"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="10"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="23"/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition Height="10"/>
<RowDefinition Height="20"/>
<RowDefinition Height="10"/>
<RowDefinition Height="15"/>
<RowDefinition Height="5"/>
<RowDefinition Height="15"/>
<RowDefinition Height="5"/>
<RowDefinition Height="15"/>
<RowDefinition Height="5"/>
<RowDefinition Height="15"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="5"/>
<ColumnDefinition Width="100"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="3" Text="Code" Foreground="#FF555555" FontSize="10"/>
<Label Grid.Row="3" Grid.Column="1" Content=":" Foreground="#FF555555" FontSize="10"/>
<TextBlock Grid.Row="3" Grid.Column="2" Text="{Binding Name}" Foreground="#FF555555" FontSize="10" />
<TextBlock Grid.Row="5" Text="Serial Number" Foreground="#FF555555" FontSize="10"/>
<Label Grid.Row="5" Grid.Column="1" Content=":" Foreground="#FF555555" FontSize="10"/>
<TextBlock Grid.Row="5" Grid.Column="2" Text="{Binding Age}" Foreground="#FF555555" FontSize="10"/>
<TextBlock Grid.Row="7" Text="Age" Foreground="#FF555555" FontSize="10"/>
<Label Grid.Row="7" Grid.Column="1" Content=":" Foreground="#FF555555" FontSize="10"/>
<TextBlock Grid.Row="7" Grid.Column="2" Text="{Binding Mail}" Foreground="#FF555555" FontSize="10"/>
</Grid>
<Button x:Name="OKButton" Grid.Column="3" Content="OK" VerticalAlignment="Bottom" Margin="0,0,0,10" >
</Button>
<Button x:Name="Cancelbutton" Grid.Column="5" Content="Cancel" VerticalAlignment="Bottom" Margin="0,0,0,10">
</Button>
<Button x:Name="FirstDeleteButton" Grid.Column="6" VerticalAlignment="top" Margin="3,5,2,0" />
</Grid>
</Border>
</Border>
</Grid>
<DataTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="FirstNormalBorder" Property="Background" Value="Green"/>
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="FirstNormalBorder" Property="Background" Value="Red"/>
</Trigger>
</DataTemplate.Triggers>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
cs:
public MainWindow()
{
InitializeComponent();
List<User> items = new List<User>();
items.Add(new User() { Name = "John Doe", Age = 42, Mail = "john@doe-family.com" });
items.Add(new User() { Name = "Jane Doe", Age = 39, Mail = "jane@doe-family.com" });
items.Add(new User() { Name = "Sammy Doe", Age = 13, Mail = "sammy.doe@gmail.com" });
lvDataBinding.ItemsSource = items;
}
}
public class User
{
public string Name { get; set; }
public int Age { get; set; }
public string Mail { get; set; }
}
ItemsControl
不跟踪SelectedItem。您可以使用ListBox
<ListBox HorizontalAlignment="Center"
Name="lvDataBinding">
<ListBox.Resources>
<!-- Removes default blue background for selected item -->
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
Color="Transparent" />
</ListBox.Resources>
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Margin="10,0,5,0"
Width="500">
<Grid.RowDefinitions>
<RowDefinition Height="120*" />
<RowDefinition Height="5" />
<RowDefinition Height="120*" />
<RowDefinition Height="5" />
<RowDefinition Height="120*" />
<RowDefinition Height="5" />
<RowDefinition Height="120*" />
</Grid.RowDefinitions>
<Border x:Name="FirstSelectionBorder"
BorderBrush="#FF0087AA">
<Border x:Name="FirstNormalBorder"
BorderBrush="#FF555555"
BorderThickness="1">
<Grid x:Name="FirstGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="10" />
<ColumnDefinition Width="300*" />
<ColumnDefinition Width="5" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="10" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="23" />
</Grid.ColumnDefinitions>
<Grid Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition Height="10" />
<RowDefinition Height="20" />
<RowDefinition Height="10" />
<RowDefinition Height="15" />
<RowDefinition Height="5" />
<RowDefinition Height="15" />
<RowDefinition Height="5" />
<RowDefinition Height="15" />
<RowDefinition Height="5" />
<RowDefinition Height="15" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" />
<ColumnDefinition Width="5" />
<ColumnDefinition Width="100" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="3"
Text="Code"
Foreground="#FF555555"
FontSize="10" />
<Label Grid.Row="3"
Grid.Column="1"
Content=":"
Foreground="#FF555555"
FontSize="10" />
<TextBlock Grid.Row="3"
Grid.Column="2"
Text="{Binding Name}"
Foreground="#FF555555"
FontSize="10" />
<TextBlock Grid.Row="5"
Text="Serial Number"
Foreground="#FF555555"
FontSize="10" />
<Label Grid.Row="5"
Grid.Column="1"
Content=":"
Foreground="#FF555555"
FontSize="10" />
<TextBlock Grid.Row="5"
Grid.Column="2"
Text="{Binding Age}"
Foreground="#FF555555"
FontSize="10" />
<TextBlock Grid.Row="7"
Text="Age"
Foreground="#FF555555"
FontSize="10" />
<Label Grid.Row="7"
Grid.Column="1"
Content=":"
Foreground="#FF555555"
FontSize="10" />
<TextBlock Grid.Row="7"
Grid.Column="2"
Text="{Binding Mail}"
Foreground="#FF555555"
FontSize="10" />
</Grid>
<Button x:Name="OKButton"
Grid.Column="3"
Content="OK"
VerticalAlignment="Bottom"
Margin="0,0,0,10">
</Button>
<Button x:Name="Cancelbutton"
Grid.Column="5"
Content="Cancel"
VerticalAlignment="Bottom"
Margin="0,0,0,10">
</Button>
<Button x:Name="FirstDeleteButton"
Grid.Column="6"
VerticalAlignment="top"
Margin="3,5,2,0" />
</Grid>
</Border>
</Border>
</Grid>
<DataTemplate.Triggers>
<Trigger Property="IsMouseOver"
Value="True">
<Setter TargetName="FirstNormalBorder"
Property="Background"
Value="Green" />
</Trigger>
<DataTrigger Binding="{Binding Path=IsSelected, RelativeSource={
RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem }}}"
Value="True">
<Setter TargetName="FirstNormalBorder" Property="Background"
Value="Red" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>