Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从两个不同的源绑定datagrid_C#_Wpf_Visual Studio_Xaml - Fatal编程技术网

C# 从两个不同的源绑定datagrid

C# 从两个不同的源绑定datagrid,c#,wpf,visual-studio,xaml,C#,Wpf,Visual Studio,Xaml,我需要在wpf的另一个窗口上显示一个数据网格中两个不同源的结果。或许我做错了。 所以我有这个模型 class StoreModel { public int ID { get; set; } public string StrName { get; set; } public TimeSpan TimeI { get; set; } public TimeSpan TimeO { get; set; } pu

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

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>

断然的
发布
悬而未决的

我的任务是在另一个页面的组合框中显示这些记录和所选选项(从3、'Resolved'、'Issued'、'Pending')。 我不知道如何在另一个页面的数据网格中包含组合框中的这两项

我试着用我自己愚蠢的方式去做,我在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++;
        }
    }
公共部分类存储页:窗口
{
列出主要模式;
字符串[]操作;
静态int-selectedRow;
静态int i=0;
公共存储页()
{
初始化组件();
this.Loaded+=StorePage\u Loaded;
actions=新字符串[2];
MainstrModel=新列表();
}
已加载无效存储页(对象发送方,路由目标e)
{
selectedRow=main窗口。selectedRow;
添加(newstoremodel(){ID=1,StrName=“Store1”,TimeI=DateTime.Now.TimeOfDay,TimeO=DateTime.Now.TimeOfDay,Actions=Actions[i]});
添加(newstoremodel(){ID=1,StrName=“Store2”,TimeI=DateTime.Now.TimeOfDay,TimeO=DateTime.Now.TimeOfDay,Actions=Actions[i]});
添加(newstoremodel(){ID=2,StrName=“Store3”,TimeI=DateTime.Now.TimeOfDay,TimeO=DateTime.Now.TimeOfDay,Actions=Actions[i]});
添加(newstoremodel(){ID=3,StrName=“Store4”,TimeI=DateTime.Now.TimeOfDay,TimeO=DateTime.Now.TimeOfDay,Actions=Actions[i]});
strGrid.ItemsSource=MainstrModel.Where(x=>x.ID==selectedRow);
}
私有无效按钮\u单击(对象发送者,路由目标e)
{
新建主窗口().Show();
这个。关闭();
}
私有无效按钮\u单击\u 1(对象发送者,路由目标)
{
DetailPage detail=新的DetailPage();
detail.detailGrid.ItemsSource=MainstrModel.Where(x=>x.ID==selectedRow);
detail.Show();
这个。关闭();
}
私有无效操作组合_SelectionChanged(对象发送方,SelectionChangedEventArgs e)
{
ComboBox combo=发件人作为ComboBox;
string item=combo.SelectedItem.ToString();
actions[i]=item.Substring(item.IndexOf(':')+1);
i++;
}
}

我应该怎么做才能实现它呢?

不要使
静态int i=0,这就是您获得越界异常的原因。不清楚如何处理操作和选定的组合框值。您可以按如下方式获取所选组合框项的索引:

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。

不要使
静态int i=0,这就是您获得越界异常的原因。不清楚如何处理操作和选定的组合框值。您可以按如下方式获取所选组合框项的索引:

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。

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


然后,您可以在新窗口的构造函数中传递Actions属性值或根据Actions属性值过滤的数据,或者根据是否显示新页面/窗口/对话框来设置它的某些属性。

首先要从组合框中获取所选值,请将SelectedItem/SelectedValue绑定为
SelectedValue={Binding DataContext.Actions,ElementName=ActionCombo}
,这将以以下形式提供用户在Actions属性中的操作:“
System.Windows.Controls.ComboBoxItem:Pending


然后,您可以在新窗口的构造函数中传递Actions属性值或根据Actions属性值过滤的数据,或者根据是否显示新页面/窗口/对话框来设置它的某些属性。

我需要的是int index=strGrid.SelectedIndex;而不是combo.SelectedIndex,因为它获取项目的索引b但不是行的(我需要)。谢谢你的想法。关于主要问题,我在另一页上没有combobox,只有一个显示结果的texblock,所以我无法处理selectedIndexGrid没有要选择的索引,除了选中的行。这是你想要的吗?我需要的是int index=strGrid.SelectedIndex;而不是combo.SelectedIndex,因为它获取项目的索引,但是不是争吵(我需要)。谢谢你的主意。关于主要的公关