私は現在、サーバーに接続されたプレイヤーを表すリスト ボックスを持つプロジェクトに取り組んでいます。リスト ボックスの項目をテンプレート化して、プレーヤーの写真とその名前が含まれるようにしました。マウスが領域に入ると、それらの要素の周りに灰色の境界線が表示されます。
私の問題は、リストが更新されたときにユーザーがリスト ボックス項目の 1 つにマウスを置いている場合 (たとえば、新しいユーザーがサーバーに接続したとき)、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 の呼び出しの直後にトリガーされます。そのエラーの説明は翻訳されているので、ベルが鳴らなかったら教えてください。とにかく、それが表示される理由はありますか?テンプレート化されたリスト ボックス アイテムのトリガーについて不明な点はありますか? アイテムが既に削除されているときに、フェードアウト アニメーションが呼び出されている可能性がありますか? どうすればこれを機能させることができますか?