组合框项目直到第1列排序后才会显示

时间:2019-08-25 05:26:19

标签: c# wpf sorting combobox datagrid

在第1列项目被排序后,单击第1列的标题才显示第2列项目“点设置”。该代码的目标是链接第一和第二列项目,然后将第二列项目用作搜索关键字。

我是C#和WPF的新手。 我很累于在第一列项目(1、2等)的前面放置顺序号,因为我认为如果这些项目最初进行排序会解决问题。但是,没有运气。我听说ObservableCollection <>不能管理输入顺序,所以一旦用List <>更改了输入顺序。但是它也不能解决这个问题。

实际上,我不想对第一栏进行排序;它们应该是固定的,根本不需要更改订单/编号。

为避免混淆,让我展示我的完整代码(对不起)。

MainWindow.xaml:

<Window x:Class="XY.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:XY"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="454.4">
    <Grid>
        <DataGrid ItemsSource="{Binding channels}"
            SelectedItem="{Binding SelectedRow, Mode=TwoWay}"
                  Margin="0,0,0,-0.2">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding name}"
                                    Header="Channel" Width="Auto"/>
                <DataGridTemplateColumn Width="100" Header="Point Setting">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox x:Name="piontsComboBox"
                                      ItemsSource="{Binding DataContext.points,
                                RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
                                      SelectionChanged="PrintText"
                                      DisplayMemberPath="name" 
                                      SelectedValuePath="name"
                                      Margin="5"
                                      SelectedItem="{Binding DataContext.SelectedPoint,
                                RelativeSource={RelativeSource AncestorType={x:Type Window}},
                                Mode=TwoWay}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
        <TextBox x:Name="tb" Width="140" Height="30" Margin="10,250,200,30"></TextBox>
        <Button x:Name="Browse_Button" Content="Browse" Margin="169,255,129.6,0"
                    Width="75" Click="Browse_Button_Click" Height="30" VerticalAlignment="Top"/>
    </Grid>
</Window>

MainWindow.xaml.cs:

using System;
using System.Windows;
using System.Windows.Controls;

namespace XY
{
    public partial class MainWindow : Window
    {
        public GridModel gridModel { get; set; }
        public MainWindow()
        {
            InitializeComponent();
            gridModel = new GridModel();
            this.DataContext = gridModel;
        }

        private void Browse_Button_Click(object sender, RoutedEventArgs e)
        {
            WakeupClass clsWakeup = new WakeupClass();
            clsWakeup.BrowseFile += new EventHandler(gridModel.ExcelFileOpen);
            clsWakeup.Start();
        }

        void PrintText(object sender, SelectionChangedEventArgs args)
        {
            var comboBox = sender as ComboBox;
            var selectedPoint = comboBox.SelectedItem as Point;
            tb.Text = selectedPoint.name;
        }
    }

    public class WakeupClass
    {
        public event EventHandler BrowseFile;
        public void Start()
        {
            BrowseFile(this, EventArgs.Empty);
        }
    }
}

ViewModelBase.cs:

using System.ComponentModel;

namespace XY
{
    public class ViewModelBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(string name)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(name));
            }
        }
    }
}

Point.cs:

namespace XY
{
    public class Point : ViewModelBase
    {
        private string _name;
        public string name
        {
            get { return _name; }
            set
            {
                _name = value;
                OnPropertyChanged("name");
            }
        }

        private int _code;
        public int code
        {
            get { return _code; }
            set
            {
                _code = value;
                OnPropertyChanged("code");
            }
        }
    }
}

Record.cs:

namespace XY
{
    public class Record : ViewModelBase
    {
        private string _name;
        public string name
        {
            get { return _name; }
            set
            {
                _name = value;
                OnPropertyChanged("name");
            }
        }

        private int _PointCode;
        public int PointCode
        {
            get { return _PointCode; }
            set
            {
                _PointCode = value;
                OnPropertyChanged("PointCode");
            }
        }

        private Record _selectedRow;
        public Record selectedRow
        {
            get { return _selectedRow; }
            set
            {
                _selectedRow = value;
                OnPropertyChanged("SelectedRow");
            }
        }

        private Point _selectedPoint;
        public Point SelectedPoint
        {
            get { return _selectedPoint; }
            set
            {
                _selectedPoint = value;
                _selectedRow.PointCode = _selectedPoint.code;
                OnPropertyChanged("SelectedRow");
            }
        }
    }
}

GridModel.cs:

using System.Collections.ObjectModel;
using System.Windows;

namespace XY
{
    public class GridModel : ViewModelBase
    {
        public ObservableCollection<Record> channels { get; set; }
        public ObservableCollection<Point> points { get; set; }
        public GridModel()
        {
            channels = new ObservableCollection<Record>() {
                new Record {name = "1. High"},
                new Record {name = "2. Middle"},
                new Record {name = "3. Low"}
            };
        }

        internal void ExcelFileOpen(object sender, System.EventArgs e)
        {
            points = new ObservableCollection<Point> { new Point { } };

            MessageBox.Show("Please assume that Excel data are loaded here.");
            points.Add(new Point { name = "point1", code = 1 });
            points.Add(new Point { name = "point2", code = 2 });
            points.Add(new Point { name = "point3", code = 3 });
            points.Add(new Point { name = "point4", code = 4 });
        }
    }
}

过程如下:

  1. 单击“浏览”按钮以加载数据。

  2. 单击第一列“频道”以对列表进行排序(目标:我想摆脱这一步)。

  3. 单击“点设置”组合框以选择项目(point1,point2,...等)。

...我不知道ObservableCollection <>在这里是否合适。如果List <>或其他任何类型更好,请进行更改。任何建议都会有所帮助。预先谢谢你。

1 个答案:

答案 0 :(得分:2)

要像这样更改点appendTo(),因为在呈现UI后要设置集合的引用,所以需要触发ObservableCollection事件以更新UI。

PropertyChanged

另一种选择是首先初始化您的收藏集。

private ObservableCollection<Point> _points;
public ObservableCollection<Point> points
{
    get { return _points; }
    set
    {
        _points = value;
        OnPropertyChanged(nameof(points));
    }     
}