Datagrid WPF中的SelectedItem绑定

时间:2012-06-08 17:14:18

标签: c# wpf binding datagrid selecteditem

我有一个显示Columns的Datagrid,它是通过Code设置ItemsSource-Property而添加的。我还有两列,由XAML添加(一个Button和一个Grid中的Image和Button的组合)。 现在我正在编辑一些单元格并想要阅读它们。 在Windows窗体中,可以读取特定的单元格,但这在WPF中是不可能或不推荐的,我读过。所以我想使用SelectedItem-Binding。我的问题是,SelectedItem始终为null。也许Datagrid存在问题,因为列的“混合添加”或者我做错了。

以下是我的代码的一部分:

XAML:

 <DataGrid AutoGenerateColumns="True" Margin="12,162,12,12" Name="dgExisting" CanUserAddRows="False" RowHeight="24" VerticalGridLinesBrush="#50000000" HorizontalGridLinesBrush="#50000000" SelectionMode="Single" CellStyle="{StaticResource DataGridCell_Centering}" CellEditEnding="dgExisting_CellEditEnding" SelectedItem="{Binding SelectedItem, Mode=TwoWay}">            
        <DataGrid.Columns>
            <DataGridTemplateColumn Header="Password" Width="60" IsReadOnly="True">
                <DataGridTemplateColumn.CellTemplate>                        
                    <DataTemplate>
                        <Button Style="{StaticResource Flat}" Click="btnDgChangePassword_Click">
                            <Grid>
                                <Image Source="{Binding imgStatus}" Margin="0,0,0,0" HorizontalAlignment="Left" VerticalAlignment="Top"/>
                                <Label Content="{Binding PasswordState}" Margin="16,-5,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Height="25" />
                            </Grid>
                        </Button>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn Header="Delete" Width="50" IsReadOnly="True">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Button Click="btnDgRemove_Click" Style="{StaticResource Flat}">
                            <Image Source="Images/redcross16.png" />
                        </Button>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>

代码如何按代码添加列:

//Create Table for Gridview
DataTable dtList = new DataTable();
dtList.Columns.Add(new DataColumn("Username"));
dtList.Columns.Add(new DataColumn("PasswordState"));
dtList.Columns.Add(new DataColumn("Domain"));
dtList.Columns.Add(new DataColumn("Description"));
dtList.Columns.Add(new DataColumn("id"));

foreach (UserCredential uc in credentialList)
{
    DataRow dR = dtList.NewRow();                
    dR["Username"] = uc.getUsername();
    dR["PasswordState"] = (uc.getPassword().Length > 0 ? "yes":"no");
    dR["Domain"] = uc.getDomain();
    dR["Description"] = uc.getDescription();
    dR["id"] = uc.getId();

    dtList.Rows.Add(dR);
}

dtList.Columns.Add("imgStatus");

for (int i = 0; i < dtList.Rows.Count; i++)
{
    dtList.Rows[i]["imgStatus"] = (dtList.Rows[i]["PasswordState"].ToString() == "yes" ? "Images/key16.png" : "");
}

dgExisting.ItemsSource = dtList.DefaultView;
dgExisting.Columns[getColumnId("Password")].DisplayIndex = 2; //reorder Key-Image
dgExisting.Columns[getColumnId("PasswordState")].Visibility = System.Windows.Visibility.Hidden; 
dgExisting.Columns[getColumnId("imgStatus")].Visibility = System.Windows.Visibility.Hidden; 
dgExisting.Columns[getColumnId("id")].Visibility = System.Windows.Visibility.Hidden; 

1 个答案:

答案 0 :(得分:1)

在C#代码中,您没有创建与DataGrid项目源的绑定。

dgExisting.ItemsSource = dtList.DefaultView;

一个绑定。这就是您的SelectedItem在XAML绑定中始终为null的原因。要在代码中创建绑定,您需要执行以下操作:

var binding = new Binding("ItemsSource");
binding.Source = dtList.DefaultView;
dgExisting.SetBinding(DataGrid.ItemsSourceProperty, binding);

在C#中设置绑定WPF有点气味。可能有一个原因,你必须这样做。但是,我建议重新设计您的应用程序以更好地遵循MVVM模式,以便您可以在XAML中设置绑定。