如何将listview绑定到自定义项集合

时间:2015-05-07 20:46:07

标签: c# wpf xaml listview data-binding

如何使用WPF数据绑定将我的自定义项目集合显示在列表视图中? 我试图制作ViewModel和ViewModel操作的自定义集合,试图让这个集合显示在列表视图中。我有一个视图模型和一个自定义集合以及一个自定义项目类:

public class TranslationViewModel
{
    public TranslationViewModel() { this.translatedItems = new TransListboxCollection(); }
    public TransListboxCollection translatedItems { get; private set; }

    public void addTranslatedItem(TransListboxItem message)
    {
        translatedItems.Add(message);
    }
}

public class TransListboxCollection : BindingList<TransListboxItem>
{

    public TransListboxCollection()
    {
        //initialize
    }
}

public class TransListboxItem : INotifyPropertyChanged
{
    private String _rawString;
    private String _tString;

    public String rawString 
    {
        get { return _rawString; }
        set { _rawString = value; NotifyPropertyChanged("rawString"); } 
    }

    public String tString 
    {
        get { return _tString; }
        set { _tString = value; NotifyPropertyChanged("tString"); } 
    }


    public TransListboxItem(String value)
    {
        this.tString = value;
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public override string ToString()
    {
        return this.tString;
    }
}

我有一个以窗体形式托管的WPF元素

    public partial class wGlobal : UserControl
{
    public TranslationViewModel tvm { get; set; }

    public wGlobal()
    {

        InitializeComponent();
        this.DataContext = tvm;
    }

}

此类

的XAML代码
<UserControl
         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:local="clr-namespace:MHF_Transcoder_3" x:Class="MHF_Transcoder_3.wGlobal" 
         mc:Ignorable="d" 
         d:DesignWidth="1000" d:DesignHeight="150">
<Grid Width="1000" Height="150">
    <ListView x:Name="listView1" ItemsSource="{Binding tvm}" HorizontalAlignment="Left" Width="1000" Height="150" VerticalAlignment="Top" Background="Black" Foreground="White" RenderTransformOrigin="0.5,0.5">
        <DataTemplate>
            <TextBlock Text="{Binding tString}" ToolTipService.ToolTip="{Binding rawString}" />
        </DataTemplate>
    </ListView>
</Grid>

我将该元素托管在Windows窗体控件

 public partial class frmGlobal : Form
{
    wGlobal xamlForm;
    TranslationViewModel tvm;

    public frmGlobal()
    {
        InitializeComponent();
        tvm = new TranslationViewModel();
        xamlForm = (wGlobal)elementHost1.Child;
        xamlForm.tvm = tvm;
    }

    delegate void addMessageCallback(TransListboxItem message);
    public void addMessage(TransListboxItem message) {
            tvm.addTranslatedItem(message);
    }

}

当我启动程序并启动所有程序时,我的所有列表视图都显示为&#34; System.Windows.DataTemplate&#34;。我之前从未真正使用过WPF或数据绑定。我对所有建议和建议持开放态度。请帮我完成这个设置并正常工作。

1 个答案:

答案 0 :(得分:1)

使用ItemTemplate包装Datatemplate

<ListView x:Name="listView1" ItemsSource="{Binding translatedItems}" HorizontalAlignment="Left" Width="1000" Height="150" VerticalAlignment="Top" Background="Black" Foreground="White" RenderTransformOrigin="0.5,0.5">
 <ListView.ItemTemplate>
    <DataTemplate>
        <TextBlock Text="{Binding tString}" ToolTipService.ToolTip="{Binding rawString}" />
    </DataTemplate>
 </ListView.ItemTemplate>
</ListView>

同样由于tvm是datacontext,你必须绑定到集合&#34; translatedItems&#34;