首页 文章

WPF:将List <class>绑定到ComboBox

提问于
浏览
3

我现在已经解决了这个问题大约3个小时了,我走到了尽头 . 目前我正在尝试将列表绑定到ComboBox .

我使用了几种方法来绑定List:

代码背后:

public partial class MainWindow : Window
{
    public coImportReader ir { get; set; }

    public MainWindow()
    {          
        ir = new coImportReader();
        InitializeComponent();
    }


    private void PremadeSearchPoints(coSearchPoint sp)
    {
        //SearchRefPoint.DataContext = ir.SearchPointCollection;
        SearchRefPoint.ItemsSource = ir.SearchPointCollection;
        SearchRefPoint.DisplayMemberPath = Name;

数据已正确绑定,但DisplayMemeberPath由于某种原因返回了类的名称而不是其成员的名称 .

XAML方法返回一个空字符串...

<ComboBox x:Name="SearchRefPoint" Height="30" Width="324" Margin="0,10,0,0"
          VerticalAlignment="Top" ItemsSource="{Binding ir.SearchPointCollection}"
          DisplayMemberPath="Name">

我还尝试用MainWindow中创建的新列表填充它 . 结果在两种情况下都是一样的 .

此外,我尝试创建和ListCollectionView这是成功的,但问题是,我可以得到ComboBox项目的索引 . 我更喜欢用Id工作 . 出于这个原因,我正在寻找一个新的解决方案,我找到了:http://zamjad.wordpress.com/2012/08/15/multi-columns-combo-box/

这个例子的问题是不清楚itemsource是如何绑定的 .

Edit:

总结一下:我目前正在尝试绑定一个类(coImportReader)中定义的对象(coSearchPoints)的列表(SearchPointsCollection) .

namespace Import_Rates_Manager
{
    public partial class MainWindow : Window
    {
        public coImportReader ir;
        public coViewerControles vc;
        public coSearchPoint sp;

        public MainWindow()
        {
            InitializeComponent();
            ir = new coImportReader();
            vc = new coViewerControles();
            sp = new coSearchPoint();
            SearchRefPoint.DataContext = ir;
        }
   }
}

//in function.... 

SearchRefPoint.ItemsSource = ir.SearchPointCollection;
SearchRefPoint.DisplayMemberPath = "Name";

namespace Import_Rates_Manager
{
    public class coImportReader
    {      
        public List<coSearchPoint> SearchPointCollection = new List<coSearchPoint>();
    }
}

namespace Import_Rates_Manager
{
    public class coSearchPoint
    {
        public coSearchPoint()
        {
            string Name = "";
            Guid Id = Guid.NewGuid();
            IRange FoundCell = null;

        }
    }
}

这导致填充的组合框没有文本

3 回答

  • 1

    DisplayMemberPath 应包含集合中元素的属性名称 . 假设 SearchPointCollection 中的元素属于 SearchPoint 类型,并且此类具有属性 SearchPointName ,则应设置 DisplayMemberPath ,如下所示:

    SearchRefPoint.DisplayMemberPath = "SearchPointName";
    

    Edit:

    在您的代码中,类 coSearchPoint 具有在构造函数中定义的Field Name . Name 必须是类的属性,否则Binding无法工作 .

  • 1

    这是一个使用MVVM模式的简单示例

    XAML

    <Window x:Class="Binding_a_List_to_a_ComboBox.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 HorizontalAlignment="Left"
          VerticalAlignment="Top">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="150"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="50"/>
            <RowDefinition Height="25"/>
        </Grid.RowDefinitions>
    
        <ComboBox Grid.Column="0" Grid.Row="0" ItemsSource="{Binding SearchPointCollection , UpdateSourceTrigger=PropertyChanged}"
                  SelectedIndex="{Binding MySelectedIndex, UpdateSourceTrigger=PropertyChanged}"
                  SelectedItem="{Binding MySelectedItem, UpdateSourceTrigger=PropertyChanged}">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition/>
                            <RowDefinition/>
                            <RowDefinition/>
                        </Grid.RowDefinitions>
                        <TextBlock Text="{Binding Id}" Grid.Row="0"/>
                        <TextBlock Text="{Binding Name}" Grid.Row="1"/>
                        <TextBlock Text="{Binding Otherstuff}" Grid.Row="2"/>
                    </Grid>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>
        <Button Content="Bind NOW" Grid.Column="0" Grid.Row="1" Click="Button_Click"/>
        <TextBlock Text="{Binding MySelectedIndex, UpdateSourceTrigger=PropertyChanged}" Grid.Column="1" Grid.Row="0"/>
    
        <Grid  Grid.Column="1" Grid.Row="1"
               DataContext="{Binding MySelectedItem, UpdateSourceTrigger=PropertyChanged}">
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
    
            <TextBlock Text="{Binding Id}" Grid.Column="0"/>
            <TextBlock Text="{Binding Name}" Grid.Column="1"/>
            <TextBlock Text="{Binding SomeValue}" Grid.Column="2"/>
        </Grid>
    </Grid>
    

    代码

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Windows;
    using Import_Rates_Manager;
    
    namespace Binding_a_List_to_a_ComboBox
    {
        /// <summary>
        /// Interaktionslogik für MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                DataContext = new coImportReader();
            }    
        }
    }
    namespace Import_Rates_Manager
    {
        public class coImportReader : INotifyPropertyChanged
        {
            private List<coSearchPoint> myItemsSource;
            private int mySelectedIndex;
            private coSearchPoint mySelectedItem;
    
            public List<coSearchPoint> SearchPointCollection 
            {
                get { return myItemsSource; }
                set
                {
                    myItemsSource = value;
                    OnPropertyChanged("SearchPointCollection ");
                }
            }
    
            public int MySelectedIndex
            {
                get { return mySelectedIndex; }
                set
                {
                    mySelectedIndex = value;
                    OnPropertyChanged("MySelectedIndex");
                }
            }
    
            public coSearchPoint MySelectedItem
            {
                get { return mySelectedItem; }
                set { mySelectedItem = value;
                OnPropertyChanged("MySelectedItem");
                }
            }
    
            #region cTor
    
            public coImportReader()
            {
                myItemsSource = new List<coSearchPoint>();
                myItemsSource.Add(new coSearchPoint { Name = "Name1" });
                myItemsSource.Add(new coSearchPoint { Name = "Name2" });
                myItemsSource.Add(new coSearchPoint { Name = "Name3" });
                myItemsSource.Add(new coSearchPoint { Name = "Name4" });
                myItemsSource.Add(new coSearchPoint { Name = "Name5" });
            }
            #endregion
    
            #region INotifyPropertyChanged Member
    
            public event PropertyChangedEventHandler PropertyChanged;
            protected virtual void OnPropertyChanged(string propertyName)
            {
                PropertyChangedEventHandler handler = PropertyChanged;
                if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
            }
    
            #endregion
        }
    
        public class coSearchPoint
        {
            public Guid Id { get; set; }
            public String Name { get; set; }
            public IRange FoundCell { get; set; }    
    
            public coSearchPoint()
            {
                Name = "";
                Id = Guid.NewGuid();
                FoundCell = null;    
            }
        }
    
        public interface IRange
        {
            string SomeValue { get; }
        }
    }
    

    这是3个类:

    • MainWindow 将VM设置为其Datacontext

    • coImportReader 提供绑定属性的类

    • coSearchPoint 这只是一个容器,供您参考

    • IRange 这只是一个界面

  • 0

    你绑定的集合需要是一个属性而不是一个字段 .

    也试试这个:

    public coImportReader ir { get; set; } 
    public <type of SearchPointCollection> irCollection { get { return ir != null ? ir.SearchPointCollection : null; } }
    

    绑定到irCollection并查看您获得的错误(如果有) .

相关问题