views:

34

answers:

1

In VS 2010/.NET 4.0, I have a List<MyClass> list of a simple class as such:

public class MyClass
{        
    public string String1 { get; set; }
    public string String2 { get; set; }

    public MyClass(string string1, string string2)
    {
        String1 = string1;
        String2 = string2;
    }
}

This list is displayed in a WPF DataGrid in code behind:

Columns are added first:

List<DataGridColumn> cols = ParentClass.GetColumns();
foreach (DataGridColumn dgc in cols)
{
    myDataGrid.Columns.Add(dgc);
}

Item source is set next:

myDataGrid.ItemsSource = ParentClass.MyClassess;

MyClassess is the List

This works fine in a sense that the DG gets populated but it is not possible to select a row in the DataGrid and DataGrid.SelectedItem always returns null. The DataGrid registers mouse events but where ever the click occurs no row gets selected as if the DataGrid is disabled...

SelectionMode="Single"  
SelectionUnit="FullRow" 
IsReadOnly="False"

Does anyone have any idea what is going on here?

TIA

Here is the XAML as requested:

<TabItem Header="Sessions" GotFocus="TabSessionsGotFocus">
                    <Grid>
                        <Grid.RowDefinitions>                            
                            <RowDefinition Height="*"></RowDefinition>                            
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition></ColumnDefinition>
                            <ColumnDefinition></ColumnDefinition>
                            <ColumnDefinition></ColumnDefinition>
                        </Grid.ColumnDefinitions>

                        <DockPanel Grid.Row="0" Grid.ColumnSpan="3" Margin="0">
                            <DataGrid x:Name="dgSessions" DockPanel.Dock="Top" IsEnabled="True" AutoGenerateColumns="False" 
                                SelectionMode="Single"  SelectionUnit="FullRow" Background="WhiteSmoke" 
                                AlternatingRowBackground="Gainsboro" GridLinesVisibility="None" IsReadOnly="False"
                                ColumnHeaderHeight="25" RowHeight="21" MouseRightButtonDown="SessionsRightButtonDown" >
                                <DataGrid.ContextMenu>
                                    <ContextMenu x:Name="cmSessions">
                                        <MenuItem x:Name="miSessionActions">
                                            Action
                                        </MenuItem>                                        
                                    </ContextMenu>
                                </DataGrid.ContextMenu>
                            </DataGrid>
                        </DockPanel>
                    </Grid>
                </TabItem>
A: 

The problem was that I was loading the DataGrid when it's parent Tab GotFocus event fired and this event was firing every time the DataGrid was clicked on.

kzen