结合项目到ListBox中的多个列 [英] Binding items into ListBox multiple columns

查看:126
本文介绍了结合项目到ListBox中的多个列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我的数据添加到多列列表框,我做到了,但我想从列表框中检索数据时面临的一个难题。 ?有没有把一个对象,而不是文字到一个列表框行的方式

 < ListView控件名称=listBox1中的ItemsSource = {绑定表项}保证金=28,28,68,67的FlowDirection =从右至左MouseDoubleClick =listBox1_MouseDoubleClick> 
< ListView控件名称=listBox1中的ItemsSource ={绑定表项}保证金=28,28,68,67的FlowDirection =从右至左MouseDoubleClick =listBox1_MouseDoubleClick>
< ListView.View>
<&GridView的GT;
< GridView.Columns>
< GridViewColumn标题=一个WIDTH =100DisplayMemberBinding ={绑定路径= ID}/>
< GridViewColumn标题=BWIDTH =100DisplayMemberBinding ={绑定路径=名称}/>
< GridViewColumn标题=CWIDTH =100DisplayMemberBinding ={绑定路径= F}/>
< /GridView.Columns>
< / GridView的>
< /ListView.View>
< /&的ListView GT;
< /网格和GT;



这是代码



 公共部分类主窗口:窗口
{
公共主窗口()
{
的InitializeComponent();
}
公共密封类MyListBoxItem
{
公共字符串字段1 {搞定;组; }
公共字符串字段2 {搞定;组; }
公共字符串字段3 {搞定;组; }
}

私人无效Window_Loaded(对象发件人,RoutedEventArgs E)
{
学生ST =新的学生(1,名,什么);
listBox1.ItemsSource =新名单(新[] {ST});
}

私人无效listBox1_MouseDoubleClick(对象发件人,MouseButtonEventArgs E)
{
对象ob = listBox1.SelectedItem;
串I =((MyListBoxItem)listBox1.SelectedItem).Field1;
}
}

和这里是类学生

 班的学生
{
INT ID;
字符串名称;
串f;

公共学生(INT ID,字符串名称,串f)
{
this.id = ID;
this.name =名称;
this.f = F;
}
公众诠释ID
{
{返回ID; }
集合{ID =价值; }
}
公共字符串名称
{
{返回名称; }
集合{名称=值; }
}
公共字符串F
{
得到{F; }
集合{F =价值; }
}
}


解决方案

不要使用listBox1.Items.Add(....)。而使用listBox1.ItemsSource =新名单(新[] {ST});



然后改变你的DisplayMemberBindings为ID,名称分别为



有不需要的ListBoxItem的类



==编辑==



您非常接近得到它完美。我已经把它贴在它下面应该如何工作。需要注意的重要的事情是在ListView进行的ItemsSource和SelctedITem,并设置IsSynchronisedWithCurrentItem为true绑定。



此外,在上下两排格子国税发,我已经示出的使用/符号结合到所选择的项目,一是两种不同的方法,并在视图模型的其他使用属性



XAML



 <窗口x:类=StackOverflow11087468.MainWindow
的xmlns =http://schemas.microsoft.com/winfx/2006/ XAML /演示
的xmlns:X =http://schemas.microsoft.com/winfx/2006/xaml
标题=主窗口HEIGHT =350WIDTH =525>
<网格和GT;
< Grid.RowDefinitions>
< RowDefinition高度=*/>
< RowDefinition高度=自动/>
< RowDefinition高度=自动/>
< /Grid.RowDefinitions>
< ListView控件名称=listBox1中
Grid.Row =0
的ItemsSource ={结合学生}
的SelectedItem ={结合SelectedStudent}
IsSynchronizedWithCurrentItem =真
保证金=28,28,68,67
的FlowDirection =从右至左>
< ListView.View>
<&GridView的GT;
< GridView.Columns>
< GridViewColumn标题=一个
WIDTH =100
DisplayMemberBinding ={绑定路径= ID}/>
< GridViewColumn标题=B
WIDTH =100
DisplayMemberBinding ={绑定路径=名称}/>
< GridViewColumn标题=C
WIDTH =100
DisplayMemberBinding ={绑定路径= F}/>
< /GridView.Columns>
< / GridView的>
< /ListView.View>
< /&的ListView GT;

< StackPanel的Grid.Row =1方向=横向>
< TextBlock的> ID< / TextBlock的>
<文本框的文本={结合学生/ ID}/>
< / StackPanel的>

< StackPanel的Grid.Row =2
方向=横向>
< TextBlock的> ID< / TextBlock的>
<文本框的文本={结合SelectedStudent.ID}/>
< / StackPanel的>
< /网格和GT;
< /窗GT;



Main.Window.cs



 使用系统; 
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;使用System.Windows
;使用System.Windows.Controls的
;
使用System.Windows.Data;使用System.Windows.Documents
;
使用System.Windows.Input;使用System.Windows.Media
;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;使用System.Windows.Shapes
;

命名空间StackOverflow11087468
{
///<总结> $ B $为MainWindow.xaml
///< b ///交互逻辑; /总结>
公共部分类主窗口:窗口
{
公共主窗口()
{
的InitializeComponent();

this.DataContext =新视图模型();
}
}
}



ViewModel.cs



 使用系统; 
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;
使用System.Collections.ObjectModel;
使用System.ComponentModel;

命名空间StackOverflow11087468
{
公共类视图模型:INotifyPropertyChanged的
{
公众的ObservableCollection<的Student GT;学生{搞定;组; }

公共视图模型()
{
this.Students =新的ObservableCollection<的Student GT;();
Students.Add(新学生(98760987,Student1,F));
Students.Add(新学生(98760988Student22,M));
}

公益助学SelectedStudent
{
{返回_selectedStudent; }

{
_selectedStudent =价值;
RaisePropertyChanged(SelectedStudent);
}
}

私人无效RaisePropertyChanged(字符串propertyName的)
{
如果(this.PropertyChanged!= NULL)
this.PropertyChanged (这一点,新PropertyChangedEventArgs(propertyName的));
}

公共事件PropertyChangedEventHandler的PropertyChanged;
私人学生_selectedStudent;
}
}


I am trying to add my data into multiple columns ListBox, I did it but I am facing a hard problem when trying to retrieve the Data from the list box. is there a way to put an object instead of text into a listBox row?

<ListView Name="listBox1" ItemsSource="{Binding Items}" Margin="28,28,68,67" FlowDirection="RightToLeft" MouseDoubleClick="listBox1_MouseDoubleClick">
        <ListView Name="listBox1" ItemsSource="{Binding Items}" Margin="28,28,68,67" FlowDirection="RightToLeft" MouseDoubleClick="listBox1_MouseDoubleClick">
        <ListView.View>
            <GridView>
                <GridView.Columns>
                    <GridViewColumn Header="a" Width="100" DisplayMemberBinding="{Binding Path=ID}" />
                    <GridViewColumn Header="b" Width="100" DisplayMemberBinding="{Binding Path=Name}" />
                    <GridViewColumn Header="c" Width="100" DisplayMemberBinding="{Binding Path=F}" />
                </GridView.Columns>
            </GridView>
        </ListView.View>
    </ListView>
</Grid>

and this is the code

 public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }
    public sealed class MyListBoxItem
    {
        public string Field1 { get; set; }
        public string Field2 { get; set; }
        public string Field3 { get; set; }
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        Students st = new Students(1, "name","anything");
        listBox1.ItemsSource = new List(new[] { st });
    }

    private void listBox1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        object ob = listBox1.SelectedItem;
        string i = ((MyListBoxItem)listBox1.SelectedItem).Field1;
    }
}

and here is the class Students

 class Students
{
    int id;
    string name;
    string f;

    public Students(int id, string name,string f)
    {
        this.id = id;
        this.name = name;
        this.f = f;
    }
    public int ID
    {
        get { return id; }
        set { id = value; }
    }
    public string Name
    {
        get { return name; }
        set { name = value; }
    }
    public string F
    {
        get { return f; }
        set { f = value; }
    }
}

解决方案

Don't use listBox1.Items.Add(....). Rather use listBox1.ItemsSource = new List(new[] {st});

Then change Your DisplayMemberBindings to "Id", "Name" respectively.

There is no need for the ListBoxItem Class.

== EDIT ==

You were very close to getting it perfectly. I've attached below how it should work. The important things to notice are the Bindings in the ListView for ItemsSource and SelctedITem, and setting IsSynchronisedWithCurrentItem to true.

Also, in the bottom two rows ofthe grid, I've shown two different ways of binding to the selected item, one using "/" notation, and the other using a property on the ViewModel

XAML

<Window x:Class="StackOverflow11087468.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
            <ListView Name="listBox1"
                      Grid.Row="0"
                      ItemsSource="{Binding Students}"
                      SelectedItem="{Binding SelectedStudent}"
                      IsSynchronizedWithCurrentItem="True"
                      Margin="28,28,68,67"
                      FlowDirection="RightToLeft">
                <ListView.View>
                    <GridView>
                        <GridView.Columns>
                            <GridViewColumn Header="a"
                                            Width="100"
                                            DisplayMemberBinding="{Binding Path=ID}" />
                            <GridViewColumn Header="b"
                                            Width="100"
                                            DisplayMemberBinding="{Binding Path=Name}" />
                            <GridViewColumn Header="c"
                                            Width="100"
                                            DisplayMemberBinding="{Binding Path=F}" />
                        </GridView.Columns>
                    </GridView>
                </ListView.View>
            </ListView>

        <StackPanel Grid.Row="1" Orientation="Horizontal">
            <TextBlock>ID</TextBlock>
            <TextBox Text="{Binding Students/ID}" />            
        </StackPanel>

        <StackPanel Grid.Row="2"
                    Orientation="Horizontal">
            <TextBlock>ID</TextBlock>
            <TextBox Text="{Binding SelectedStudent.ID}" />
        </StackPanel>
    </Grid>
</Window>

Main.Window.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace StackOverflow11087468
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            this.DataContext = new ViewModel();
        }
    }
}

ViewModel.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace StackOverflow11087468
{
    public class ViewModel : INotifyPropertyChanged
    {
        public ObservableCollection<Student> Students { get; set; }

        public ViewModel()
        {
            this.Students = new ObservableCollection<Student>();
            Students.Add(new Student(98760987, "Student1", "F"));
            Students.Add(new Student(98760988, "Student22", "M"));
        }

        public Student SelectedStudent
        {
            get { return _selectedStudent; }
            set
            {
                _selectedStudent = value;
                RaisePropertyChanged("SelectedStudent");
            }
        }

        private void RaisePropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private Student _selectedStudent;
    }
}

这篇关于结合项目到ListBox中的多个列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆