简体   繁体   中英

Binding datagrid from two different sources

I need to show results of two different sources in one data grid on another window in W PF. Or probably I'm doing it wrong. so i have this model

class StoreModel
    {
        public int ID { get; set; }
        public string StrName { get; set; }
        public TimeSpan TimeI { get; set; }
        public TimeSpan TimeO { get; set; }
        public string Actions { get; set; }//Added later to see if it works
    }

and this view

<DataGrid x:Name="strGrid" AutoGenerateColumns="False" CanUserAddRows="False">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Store" Binding="{Binding StrName}" Width="*"/>
                <DataGridTextColumn Header="TimeIn" Binding="{Binding TimeI}" Width="*"/>
                <DataGridTextColumn Header="TimeOut" Binding="{Binding TimeO}" Width="*"/>
                <DataGridTemplateColumn Header="Action" Width="*" x:Name="comboTemp">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate x:Name="myTemplate">
                            <ComboBox x:Name="ActionCombo" IsReadOnly="True" IsEditable="True" Text="Select Action" SelectionChanged="ActionCombo_SelectionChanged">
                                <ComboBoxItem>Resolved</ComboBoxItem>
                                <ComboBoxItem>Issued</ComboBoxItem>
                                <ComboBoxItem>Pending</ComboBoxItem>
                            </ComboBox>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>

在此处输入图片说明

My task is to just show these records and selected option(from 3,'Resolved','Issued','Pending') from combo box on another page. I don't know how to include these 2 items from combo box in another page's data grid.

在此处输入图片说明

I tried to do it my own silly way, i retrieved two combo box's values in an array on selection_changed event(which lead to another issue of index out of bound exception if user try to change the selection third time, any help on this too?) and tried to bind these two source together and didn't work.

public partial class StorePage : Window
    {
        List<StoreModel> MainstrModel;
        string[] actions ;
        static int selectedRow;
        static int i = 0;
        public StorePage()
        {
            InitializeComponent();
            this.Loaded += StorePage_Loaded;
           actions = new string[2];
            MainstrModel = new List<StoreModel>();

        }

        void StorePage_Loaded(object sender, RoutedEventArgs e)
        {
            selectedRow = MainWindow.selectedRow;

            MainstrModel.Add(new StoreModel() { ID = 1, StrName = "Store1", TimeI = DateTime.Now.TimeOfDay, TimeO = DateTime.Now.TimeOfDay, Actions = actions[i] });
            MainstrModel.Add(new StoreModel() { ID = 1, StrName = "Store2", TimeI = DateTime.Now.TimeOfDay, TimeO = DateTime.Now.TimeOfDay, Actions = actions[i] });
            MainstrModel.Add(new StoreModel() { ID = 2, StrName = "Store3", TimeI = DateTime.Now.TimeOfDay, TimeO = DateTime.Now.TimeOfDay, Actions = actions[i] });
            MainstrModel.Add(new StoreModel() { ID = 3, StrName = "Store4", TimeI = DateTime.Now.TimeOfDay, TimeO = DateTime.Now.TimeOfDay, Actions = actions[i] });

            strGrid.ItemsSource = MainstrModel.Where(x => x.ID == selectedRow);

        }


        private void Button_Click(object sender, RoutedEventArgs e)
        {
            new MainWindow().Show();
            this.Close();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            DetailPage detail = new DetailPage();

                detail.detailGrid.ItemsSource = MainstrModel.Where(x => x.ID == selectedRow);


            detail.Show();
            this.Close();
        }



        private void ActionCombo_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBox combo = sender as ComboBox;
            string item = combo.SelectedItem.ToString();
            actions[i] = item.Substring(item.IndexOf(':') + 1);
                i++;
        }
    }

what should i do to achieve it?

Don't make static int i = 0; , this why you're getting Out of Bounds exception. Not clear how you will handle actions and selected ComboBox value. You can obtain index of selected combobox item like this:

ComboBox combo = sender as ComboBox;
int index combo.SelectedIndex;
if (index == -1)
    return; //nothing is selected
actions[index] = item.Substring(item.IndexOf(':') + 1);

Better solution is to populate combobox from some data(to have relation selected index - particular action) source and get selected item when selection is changed. Then, when you want to pass the data source to another view, you can safely pass existing datasource in a new view and set combox's SelectedIndex in, for example, DataGrid.LoadingRow.

First to get selected value from ComboBox, bind SelectedItem / SelectedValue as SelectedValue={Binding DataContext.Actions, ElementName=ActionCombo} , this will give you user's action in Actions property in the form : " System.Windows.Controls.ComboBoxItem: Pending ".

Then you can pass either the Actions property value or the filtered data according to Actions property value, either in new window's constructor, or set some property of it depending upon whether you showing a new Page/Window/Dialog.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM