我目前正在开发一个项目,该项目有一个表示连接到服务器的玩家的列表框。我模板化列表框项目,以便它们包含播放器及其名称的图片,当鼠标进入该区域时,这些元素周围会出现灰色边框。
我的问题是,如果用户在该列表更新后将鼠标悬停在其中一个列表框项目上(假设新用户连接到服务器时),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之后触发的。该错误描述已翻译,所以请告诉我它是否没有响铃。无论如何,任何想法为什么会出现?有没有关于模板化列表框项目的触发器我不知道的事情?可能是项目已被删除时调用淡出动画?我怎样才能做到这一点?
答案 0 :(得分:0)
我认为您的问题与刷新列表的方式有关。您应该尝试设置ItemsSource一次,然后只是操作其中的数据,而不是清除列表并重新分配ItemsSource。如果您使用ObservableCollection<>
作为来源,则无需拨打任何Refresh()
。刷新将自动进行。