我目前正在开发一个项目,该项目有一个列表框,表示连接到服务器的玩家。我对列表框项目进行了模板化,以便它们包含播放器的图片及其名称,当鼠标进入该区域时,这些元素周围会出现灰色边框。
我的问题是,如果用户在列表更新时将鼠标悬停在其中一个列表框项目上(假设当新用户连接到服务器时),Visual Studio 会抛出一个与我的鼠标悬停机制有关的错误,告诉我它在 XAML 中找不到名称。
这是我的列表框项目模板
<ListBox.ItemTemplate>
<DataTemplate >
<Border BorderThickness="3" CornerRadius="3" Margin="2,2,0,0" Cursor="Hand">
<Border.BorderBrush >
<SolidColorBrush x:Name="BorderBackgroundColor" />
</Border.BorderBrush >
<Grid VerticalAlignment="Center" Margin="0,0,0,0">
<Grid.Background>
<SolidColorBrush x:Name="GridBackgroundColor" />
</Grid.Background>
<Grid.Triggers >
<EventTrigger RoutedEvent="Grid.MouseEnter" >
<BeginStoryboard>
<Storyboard>
<ColorAnimation Duration="0:0:0.2" Storyboard.TargetName="GridBackgroundColor" Storyboard.TargetProperty="Color" To="Gray" />
<ColorAnimation Duration="0:0:0.2" Storyboard.TargetName="BorderBackgroundColor" Storyboard.TargetProperty="Color" To="Gray" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="Grid.MouseLeave" >
<BeginStoryboard>
<Storyboard>
<ColorAnimation Duration="0:0:0.2" Storyboard.TargetName="GridBackgroundColor" Storyboard.TargetProperty="Color" To="Transparent" />
<ColorAnimation Duration="0:0:0.2" Storyboard.TargetName="BorderBackgroundColor" Storyboard.TargetProperty="Color" To="Transparent" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Grid.Triggers>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
</Grid.RowDefinitions>
<Border Grid.Column="0" Grid.Row="0" BorderThickness="2" CornerRadius="2" Height="30" >
<Border.BorderBrush>
<MultiBinding Converter="{StaticResource StateToBorderConverter}" >
<Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}" />
<Binding Path="State" />
</MultiBinding>
</Border.BorderBrush>
<Image Source="{Binding Path=imagePath}" VerticalAlignment="Bottom" HorizontalAlignment="Left" />
</Border>
<Label Grid.Column="1" Grid.Row="0" Content="{Binding Path=profileName}" VerticalContentAlignment="Bottom" HorizontalAlignment="Left" VerticalAlignment="Bottom" Margin="5,0,0,0" FontSize="15" />
</Grid>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
这是刷新方法
private void ActualisePlayerList( PlayerListMessage playerListMessage )
{
playersProfileInfo.Clear();
foreach (Utilitaire.ClientProperties oneProperty in playerListMessage.Clients)
{
string profileName = oneProperty.Username;
string imagePath = Directory.GetCurrentDirectory() + "//Profile" + oneProperty.Username.ToLower() + ".jpg";
if (!File.Exists(imagePath))
{
imagePath = "ImageRessources/anonymous-icon.jpg";
}
playersProfileInfo.Add(new ProfileContainer(imagePath, profileName, oneProperty.State));
}
PlayersListBox.ItemsSource = playersProfileInfo;
PlayersListBox.Items.Refresh();
Filter("");
}
这是被抛出的命令
Cannot find the name 'GridBackgroundColor' in the name range of 'System.Windows.Controls.Grid'.
如前所述,错误是在调用 ActualisePlayerList 之后触发的。该错误描述已翻译,所以请告诉我它是否没有响铃。无论如何,任何想法为什么会出现?关于模板化列表框项上的触发器,我有什么不知道的吗?当项目已经被删除时,是否正在调用淡出动画?我怎样才能使这项工作?