简体   繁体   English

如何将ListView绑定到自定义项目集合

[英]How to bind listview to custom item collection

How do I get my custom item collection to show up in my list view using WPF data bindings? 如何使用WPF数据绑定使自定义项目集合显示在列表视图中? I have a tried to make a ViewModel and a custom collection that the ViewModel manipulates, in an attempt to get this collection to show up in a listview. 我试图制作一个ViewModel和一个由ViewModel操作的自定义集合,以试图使该集合显示在列表视图中。 I have a view model and a custom collection and a custom item class: 我有一个视图模型,一个自定义集合和一个自定义项目类:

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;
    }
}

I have a WPF element hosted in a windows form 我在Windows窗体中托管了WPF元素

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

    public wGlobal()
    {

        InitializeComponent();
        this.DataContext = tvm;
    }

}

The XAML code for such 此类的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>

and I have that element hosted in a windows form control 而且我将该元素托管在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);
    }

}

When I get the program up and launch everything, all my list view says is "System.Windows.DataTemplate". 当我启动程序并启动所有程序时,我所有的列表视图都显示为“ System.Windows.DataTemplate”。 I've never really worked with WPF or data bindings before. 我以前从未真正使用过WPF或数据绑定。 I'm open to any and all advice and suggestions. 我愿意接受任何建议和建议。 Please help me get this setup and properly working. 请帮助我进行此设置并正常工作。

Wrap Datatemplate with ItemTemplate 用ItemTemplate包装数据模板

<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>

Also as the tvm is the datacontext, you must bind to the collection "translatedItems" 另外,由于tvm是datacontext,因此必须绑定到集合“ translatedItems”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM