在 MahApps Metro DataGridCheckBoxColumn 中,我如何以编程方式 return 复选框值? (并修复额外的行)

In MahApps Metro DataGridCheckBoxColumn how can I programmatically return checkbox values? (and fix extra row)

我知道这不一定特定于 MahApps Metro DataGridCheckBoxColumns,但我认为这些信息可能有助于有人回答我的问题。

我正在尝试对 MahApps Metro

中的 DataGridCheckBox 列做两件事

1.我想要两个单独的数据网格,并且能够 return 选择任何一行的第二列中的值

例如,如果我有一个如下所示的数据网格:

当某人选中了与 2 关联的复选框,并且选中了与 Red 关联的复选框时,我希望消息框显示“2 Red”。

我的.xaml

        <DataGrid Name="numberGrid"
              ItemsSource="{Binding Path=numberGrid}"
              Grid.Row="0"
              AutoGenerateColumns="False"
                  Width="300"
                  Height="auto">
            <DataGrid.Columns>

                <DataGridCheckBoxColumn ElementStyle="{DynamicResource MetroDataGridCheckBox}"
                                    EditingElementStyle="{DynamicResource MetroDataGridCheckBox}"
                                    Header="Select"
                                    Binding="{Binding RelativeSource={RelativeSource AncestorType=DataGridRow}, Path=IsSelected, Mode=OneWay}"
                                    />
                <DataGridTextColumn     Header="Numbers" 
                                    Binding="{Binding Number, Mode=OneWay}"
                                        Width="*"/>
            </DataGrid.Columns>
        </DataGrid>




        <DataGrid Name="colorGrid"
              ItemsSource="{Binding Path=colorGrid}"
              Grid.Row="0"
              AutoGenerateColumns="False"
              Width="300">
            <DataGrid.Columns>
                <DataGridCheckBoxColumn ElementStyle="{DynamicResource MetroDataGridCheckBox}"
                                    EditingElementStyle="{DynamicResource MetroDataGridCheckBox}"
                                    Header="Select"
                                    Binding="{Binding RelativeSource={RelativeSource AncestorType=DataGridRow}, Path=IsSelected, Mode=OneWay}"
                                    />
                <DataGridTextColumn     Header="Colors" 
                                    Binding="{Binding Color, Mode=OneWay}"
                                        Width="*"/>
            </DataGrid.Columns>
        </DataGrid>

my .cs 注释掉了我认为应该发生的伪代码。

    ObservableCollection<MahAppsNumbers> MAnumbers = new ObservableCollection<MahAppsNumbers>
    {

        new MahAppsNumbers{Number = "1"},
        new MahAppsNumbers{Number = "2"},
        new MahAppsNumbers{Number = "3"},
        new MahAppsNumbers{Number = "4"}


    };

    public class MahAppsNumbers
    {
        public string Number { set; get; }
    }

    ObservableCollection<MahAppsAccents> MAcolors = new ObservableCollection<MahAppsColors>
    {
        new MahAppsColors{Color = "Red"},
        new MahAppsColors{Color = "Orange"},
        new MahAppsColors{Color = "Yellow"},
        new MahAppsColors{Color = "Green"}
    };

    public class MahAppsColors
    {
        public string Color { set; get; }
    }




    public myWindow()
    {




        InitializeComponent();

        numberGrid.ItemsSource = MAnumbers;
        colorGrid.ItemsSource = MAcolors;

        //Something like this maybe?  I know this isn't a thing you can do
        //string currentNumber = numberGrid.SelectedRow[1]; //to get the second column value
        //string currentColor = colorGrid.SelectedRow[1];
        //MessageBox.Show(currentNumber + currentColor);


    }

2。奖励积分,为什么最后多了一行?

我已经找到了很多解决方案,基本上都是 same。但这并不能解决我的问题。

提前致谢。

编辑 1

因为我希望每次选择一个框时都发生这种情况,所以我在 myWindow()

中尝试这样的事情
this.numberGrid.SelectedCellsChanged += new SelectedCellsChangedEventHandler(numberGrid_SelectionChanged);

并添加一个功能,希望有人能为我指出正确的修复方向。

private void numberGrid_SelectionChanged(object sender, SelectedCellsChangedEventArgs e)
{
     MessageBox.Show(numberGrid.SelectedItem.ToString());
}

尝试这样绑定,

<DataGridCheckBoxColumn  Binding="{Binding Path=IsSelected, Mode=TwoWay}" />

现在你有一个 属性 IsSelected 将它添加到你的 collection 绑定你的 DataGrid。当您检查您的 CheckBox IsSelected property Set 将调用并将其设置为 True 时,现在在您的 collection 中具有已检查的 IsSelected True。