【问题标题】:How to bind listview to custom item collection如何将列表视图绑定到自定义项目集合
【发布时间】:2015-05-07 20:46:06
【问题描述】:

如何使用 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;
    }
}

我有一个托管在 Windows 窗体中的 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);
    }

}

当我启动程序并启动所有内容时,我的所有列表视图都显示为“System.Windows.DataTemplate”。我以前从未真正使用过 WPF 或数据绑定。我愿意接受任何和所有的意见和建议。请帮助我进行此设置并正常工作。

【问题讨论】:

    标签: c# wpf xaml listview data-binding


    【解决方案1】:

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

    由于 tvm 是数据上下文,您必须绑定到集合“translatedItems”

    【讨论】:

    • 我能够用 让它显示一些东西,但现在列表视图本身仍然是空的
    • 现在看它绑定到translatedItems @BustyLoli-Chan
    • 但是将翻译项添加到集合中不会导致列表视图发生变化
    • 为什么不使用 ObservableCollection?
    • 好的,我将listview绑定到translatedItems,并添加了item模板,但是listview还是空的。我使用 BindingList 因为我在某处读到它比 ObservableCollection 有优势。我可以切换它,看看它是否有效。它没有用。
    猜你喜欢
    • 2016-02-24
    • 1970-01-01
    • 1970-01-01
    • 2011-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-08
    • 2012-08-23
    相关资源
    最近更新 更多