2

ListBoxの複数の列にデータを追加しようとしていますが、リストボックスからデータを取得しようとすると、難しい問題が発生します。テキストの代わりにオブジェクトをlistBox行に配置する方法はありますか?

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

これがコードです

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

これがクラスの生徒です

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

1 に答える 1

6

listBox1.Items.Add(....)は使用しないでください。むしろlistBox1.ItemsSource=new List(new [] {st});を使用してください。

次に、DisplayMemberBindingsをそれぞれ「Id」、「Name」に変更します。

ListBoxItemクラスは必要ありません。

==編集==

あなたはそれを完全に手に入れることに非常に近かった。私はそれがどのように機能するかを以下に添付しました。注意すべき重要なことは、ItemsSourceとSelctedITemのListViewのバインディングと、IsSynchronisedWithCurrentItemをtrueに設定することです。

また、グリッドの下の2行に、選択したアイテムにバインドする2つの異なる方法を示しました。1つは「/」表記を使用し、もう1つは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;
    }
}
于 2012-06-18T16:53:12.847 に答える