当ContentControl隐藏时,ListView项目在呈现时具有0宽度

时间:2013-11-29 21:09:29

标签: c# wpf listview contentcontrol

我遇到了ListView的问题,而它隐藏的ContentControl是隐藏的。

我的程序使用XMPP并在玩家连接到聊天服务器时进行更新。当ContentControl在某人连接时可见时,它看起来像这样:

Proper

但是如果他们连接并且ContentControl在呈现它时看起来像是不可见的(一旦再次设置可见):

NotRendered

但是,如果你只是在正确的位置悬停,你可以看到控件仍在那里,只是没有渲染:

StillThere

这是我的UserControl:

<UserControl x:Class="LegendaryClient.Controls.ChatPlayer"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             mc:Ignorable="d"
             Height="50" Width="250">
    <Grid>
        <Rectangle Fill="#02000000" />
        <!-- Allow entire control to be hovered over -->
        <Image x:Name="ProfileImage" HorizontalAlignment="Left" Height="40" Margin="5,5,0,0" VerticalAlignment="Top" Width="40" Source="/LegendaryClient;component/Icon.ico" />
        <Label x:Name="LevelLabel" Content="30" HorizontalAlignment="Left" Margin="5,20,0,0" VerticalAlignment="Top" Foreground="White" FontWeight="SemiBold">
            <Label.Effect>
                <DropShadowEffect ShadowDepth="2" BlurRadius="1" />
            </Label.Effect>
        </Label>

        <Label x:Name="PlayerName" Content="Snowl" HorizontalAlignment="Left" Margin="45,0,0,0" VerticalAlignment="Top" FontWeight="Bold" Foreground="White" />
        <Label x:Name="PlayerStatus" Content="Test status" HorizontalAlignment="Left" Margin="45,20,0,0" VerticalAlignment="Top" Foreground="White" />
    </Grid>
</UserControl>

我的渲染功能:

if (Client.UpdatePlayers)
{
    Client.UpdatePlayers = false;

    ChatListView.Items.Clear();
    foreach (KeyValuePair<string, ChatPlayerItem> ChatPlayerPair in Client.AllPlayers.ToArray())
    {
        if (ChatPlayerPair.Value.Level != 0)
        {
            ChatPlayer player = new ChatPlayer();
            player.Width = 250;
            player.Tag = ChatPlayerPair.Value;
            player.PlayerName.Content = ChatPlayerPair.Value.Username;
            player.LevelLabel.Content = ChatPlayerPair.Value.Level;
            player.PlayerStatus.Content = ChatPlayerPair.Value.Status;
            var uriSource = new Uri(Path.Combine(Client.ExecutingDirectory, "Assets", "profileicon", ChatPlayerPair.Value.ProfileIcon + ".png"), UriKind.RelativeOrAbsolute);
            player.ProfileImage.Source = new BitmapImage(uriSource);
            player.ContextMenu = (ContextMenu)Resources["PlayerChatMenu"];
            player.MouseMove += ChatPlayerMouseOver;
            player.MouseLeave += player_MouseLeave;
            ChatListView.Items.Add(player);
        }
    }
}

0 个答案:

没有答案