当列表框中的一个项目高亮显示时刷新列表框时出错
本文关键字:列表 高亮 项目 显示 刷新 出错 一个 | 更新日期: 2023-09-27 18:07:01
我目前正在做一个项目,它有一个列表框,代表连接到服务器的玩家。我对列表框项进行了模板化,以便它们包含玩家的图片及其名称,当鼠标进入该区域时,这些元素周围会出现灰色边框。
我的问题是,当列表更新时,如果用户将鼠标放在其中一个列表框项上(假设当新用户连接到服务器时),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之后触发的。这个错误描述是翻译的,如果没有什么印象,请告诉我。不管怎样,知道它为什么会出现吗?是否有一些我不知道的关于模板列表框项的触发器?当项目已经被删除时,是否会调用淡出动画?
我认为你的问题与你如何刷新列表有关。不要清除列表并重新分配ItemsSource,您应该尝试设置ItemsSource一次,然后只操作其中的数据。如果你用ObservableCollection<>
作为源,你不需要调用任何Refresh()
。刷新将自动发生