【问题标题】:listBox DataTemplate not picking up valueslistBox DataTemplate 没有拾取值
【发布时间】:2012-07-18 12:29:55
【问题描述】:

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

但是,我需要为每个项目渲染图像和一些纹理块。这是我的 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>

我的收藏类如下:

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,项目将以其名字呈现。

【问题讨论】:

  • 如果你的 person 类应该是不可变的,那么你应该做一些事情:摆脱属性设置器,将字段设置为 readonly。如果它不是不可变的,您应该实现INPC,以便在 UI 中获取更改。

标签: wpf xaml datatemplate


【解决方案1】:

没有发现绑定本身有什么问题,但是您的头像类型似乎关闭了,WPF 需要 ImageSource(我不知道 Bitmap 和 ImageSource 之间是否存在任何隐式转换,请检查 binding errors 到找出来)。

【讨论】:

  • 原来是拼写错误!我输入了名字而不是名字。你也说得对,化身的类型不兼容。由于我的 JSON 数据包含头像的 URL,我只需将 Image 的 Source 属性设置为 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-19
  • 2015-07-10
  • 2018-01-07
  • 2011-07-13
  • 2015-04-29
  • 1970-01-01
  • 2015-01-10
相关资源
最近更新 更多