C# 当列表框的某个项目高亮显示时刷新列表框时出错
我目前正在从事一个项目,该项目有一个代表连接到服务器的玩家的列表框。我对列表框项目进行了模板化,以便它们包含玩家的图片及其姓名,当鼠标进入该区域时,这些元素周围会出现灰色边框 我的问题是,如果一个用户在列表框项目更新时(比如当一个新用户连接到服务器时)将鼠标放在其中一个列表框项目上,VisualStudio会抛出一个错误,该错误与我的鼠标悬停机制有关,告诉我它在XAML中找不到名称 这是我的列表框项目模板C# 当列表框的某个项目高亮显示时刷新列表框时出错,c#,wpf,xaml,listboxitem,C#,Wpf,Xaml,Listboxitem,我目前正在从事一个项目,该项目有一个代表连接到服务器的玩家的列表框。我对列表框项目进行了模板化,以便它们包含玩家的图片及其姓名,当鼠标进入该区域时,这些元素周围会出现灰色边框 我的问题是,如果一个用户在列表框项目更新时(比如当一个新用户连接到服务器时)将鼠标放在其中一个列表框项目上,VisualStudio会抛出一个错误,该错误与我的鼠标悬停机制有关,告诉我它在XAML中找不到名称 这是我的列表框项目模板 <ListBox.ItemTemplate> <DataTemp
<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>
这是正在抛出的命令
Cannot find the name 'GridBackgroundColor' in the name range of 'System.Windows.Controls.Grid'.
如前所述,该错误是在调用ImplementsPlayerList之后触发的。那个错误描述已经翻译好了,所以请告诉我它是否有任何意义。不管怎样,你知道它为什么会出现吗?关于模板化列表框项上的触发器是否有我不知道的地方?是否在项目已移除时调用淡出动画?我怎样才能做到这一点呢?我想你的问题在于如何刷新列表。与其清除列表并重新分配ItemsSource,不如尝试设置ItemsSource一次,然后只处理其中的数据。如果使用
observateCollection
作为源,则不必调用任何Refresh()
。刷新将自动进行。对IsMouseOver
属性使用DataTrigger
而不是使用EventTriggers
。我将如何准确地将IsMouseOver属性分配给DataTrigger?我是否需要使用绑定将它链接到另一个将IsMouseOver作为proprety的元素这是一个例子你写了ClientProperties类吗?很抱歉反应太晚了,但它很有魅力。谢谢。Refresh()是作为预防措施存在的,但我删除了它和ItemsSource矫揉造作,它没有更改错误。您也将ItemsSource数据类型更改为ObservableCollection?如果使用ObservableCollection,则在删除或添加项到列表时会更新列表框。如果某个现有项发生更改,则不会发生任何事情。获取要更新的内容只是实现notifypropertychange的问题
Cannot find the name 'GridBackgroundColor' in the name range of 'System.Windows.Controls.Grid'.