【问题标题】:Binding datagrid from two different sources绑定来自两个不同来源的数据网格
【发布时间】:2015-12-04 12:34:36
【问题描述】:

我需要在 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>

我的任务是从另一个页面的组合框中显示这些记录和选择的选项(来自 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++;
        }
    }

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

【问题讨论】:

    标签: c# wpf visual-studio xaml


    【解决方案1】:

    不要让static int i = 0;,这就是为什么你会得到 Out of Bounds 异常。不清楚您将如何处理操作和选定的 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 中设置组合框的 SelectedIndex。

    【讨论】:

    • 我需要的是 int index= strGrid.SelectedIndex;不是 combo.SelectedIndex 因为它获取项目的索引而不是行的索引(我需​​要)。谢谢你的想法。关于主要问题,我在另一页上没有组合框,只是一个 texblock 来显示结果,所以我不能在 selectedIndex 上工作
    • 网格没有可供选择的索引,除了选定的行。是你想要的吗?
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多