绑定来自两个不同来源的数据网格

时间:2015-12-04 12:34:37

标签: c# wpf visual-studio xaml

我需要在W PF中的另一个窗口的一个数据网格中显示两个不同源的结果。或者我可能做错了。 所以我有这个模型

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
    }

和这个观点

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

enter image description here

我的任务是在另一个组合框中显示这些记录和选定的选项(从3,&#39;已解决&#39;,&#39;已发布&#39;待定&#39;)页。 我不知道如何在另一个页面的数据网格中包含这两个项目。

enter image description here

我尝试以自己的方式做事,我在selection_changed事件的数组中检索了两个组合框的值(如果用户尝试第三次更改选择,则会导致另一个索引超出绑定异常的问题,对此也有任何帮助吗?)并试图将这两个来源绑定在一起并且没有用。

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

我该怎么做才能实现它?

2 个答案:

答案 0 :(得分:0)

不要static int i = 0;,这就是为什么你会超越界限异常的原因。不清楚如何处理动作和选择的ComboBox值。您可以获得所选组合框项目的索引,如下所示:

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

更好的解决方案是从一些数据中填充组合框(以获得关系选择索引 - 特定操作)源,并在选择更改时获取所选项。然后,当您想要将数据源传递到另一个视图时,您可以安全地在新视图中传递现有数据源,并在例如DataGrid.LoadingRow中设置combox的SelectedIndex。

答案 1 :(得分:0)

首先从ComboBox中获取选定的值,将SelectedItem / SelectedValue绑定为SelectedValue={Binding DataContext.Actions, ElementName=ActionCombo},这将在Actions属性中以以下形式为用户提供操作:&#34; System.Windows.Controls.ComboBoxItem: Pending&# 34 ;.

然后,您可以根据Actions属性值传递Actions属性值或过滤后的数据,或者在新窗口的构造函数中传递,或根据您是否显示新的Page / Window / Dialog设置它的某些属性