jeudi 8 octobre 2015

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

I know this isn't necessarily specific to MahApps Metro DataGridCheckBoxColumns, but I figured that information may help someone answer my question.

I'm trying to do two things with my DataGridCheckBox Column in MahApps Metro

1. I would like to have two separate datagrids, and have the ability to return the value in the second column of whichever row is selected

For example, if I had a datagrid that looked like this:

enter image description here

When someone has the checkbox associated with 2 selected, and the checkbox associated with Red selected, I would like a messagebox to show "2 Red".

my .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 with the pseudocode of what I think should happen commented out.

    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. For bonus points, why do I have an extra row at the end?

I've found TONS of solutions for this, that are all basically the same. But that doesn't solve my problem.

Thanks in advance.




Aucun commentaire:

Enregistrer un commentaire