0

我正在学习使用 MSDN 中的示例将 WPF 中的 listBox 与 dataTemplate 一起使用,我可以将绑定到 ObservableCollection 的 listBox 作为源呈现并通过覆盖 ToString 方法。

但是,我需要为每个项目渲染一个图像和一些 texblocks。这是我的 XAML:

<Grid x:Class="MyAddin.WPFControls"
             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" 
             xmlns:c="clr-namespace:MyAddin"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300"
             Background="Transparent"
             HorizontalAlignment="Stretch" Width="auto"
             Height="215" VerticalAlignment="Stretch" ShowGridLines="False">
    <Grid.Resources>
        <c:People x:Key="MyFriends"/>
    </Grid.Resources>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <TextBlock HorizontalAlignment="Left" 
               IsManipulationEnabled="True" 
               Height="20" Width="300">Activity Feed</TextBlock>
    <ListBox Grid.Row="1" Name="listBox1" IsSynchronizedWithCurrentItem="True" 
             BorderThickness="0" ScrollViewer.VerticalScrollBarVisibility="Auto"
             VerticalContentAlignment="Stretch" Margin="0,0,0,5" Background="Transparent">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="60"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <Border Margin="5" BorderBrush="Black" BorderThickness="1">
                        <Image Source="{Binding Path=Avatar}" Stretch="Fill" Width="50" Height="50" />
                    </Border>
                    <StackPanel Grid.Column="1" Margin="5">
                        <StackPanel Orientation="Horizontal" TextBlock.FontWeight="Bold" >
                            <TextBlock Text="{Binding Path=Firstname }" />
                        </StackPanel>
                        <TextBlock Text="{Binding Path=Comment}" />
                    </StackPanel>
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>

我的 Collection 类如下:

public class People : ObservableCollection<Person>
{ }        

public class Person
{
    private string firstName;
    private string comment;
    private Bitmap avatar;

    public Person(string first, string comment, Bitmap avatar)
    {
        this.firstName = first;
        this.comment = comment;
        this.avatar = avatar;
    }

    public string FirstName
    {
        get { return firstName; }
        set { firstName = value; }
    }

    public string Comment
    {
        get { return comment; }
        set { comment = value; }
    }

    public Bitmap Avatar
    {
        get { return avatar;}
        set { avatar = value; }
    }

    public override string ToString()
    {
        return firstName.ToString();
    }
}

加载插件后,我将下载数据并设置 itemsSource。

People p = new People();
p.Add(new Person("Willa", "Some Comment", myAvatar));
p.Add(new Person("Isak", "Some Comment", myAvatar));
p.Add(new Person("Victor", "Some Comment", myAvatar));

this.wpfControl.listBox1.ItemsSource = p;

我面临的问题是这些项目被呈现为空行,而如果我删除 dataTemplate,则项目的名字会很好地呈现。

4

1 回答 1

0

绑定本身没有任何问题,但您的头像类型似乎关闭,WPF 预计ImageSource(我不知道 Bitmap 和 之间是否存在任何隐式转换ImageSource,请检查绑定错误以找出答案)。

于 2012-07-18T12:37:59.700 回答