WPF DataGrid,获取单元格值

时间:2015-02-20 11:53:30

标签: wpf datagrid

我需要从DataGrid的SelectedItem获取单元格值。

这是DataGrid XAML代码:

<DataGrid CanUserReorderColumns="false" CanUserAddRows="False" AutoGenerateColumns="False" Height="180" HorizontalAlignment="Left" Margin="6,11,0,0" Name="dataGridCards" VerticalAlignment="Top" Width="288" SelectionChanged="dataGridCards_SelectionChanged">
                    <DataGrid.Columns>
                        <DataGridTextColumn Header="Card ID" Binding="{Binding cardID}" IsReadOnly="True"/>
                        <DataGridTextColumn Header="Card Name" Binding="{Binding cardName}" IsReadOnly="True"/>
                        <DataGridTextColumn Header="Set Mark" Binding="{Binding setMark}" IsReadOnly="True" Width="*"/>
                    </DataGrid.Columns>
                </DataGrid>

我阻止了列重新排序并使用了:

object row = dataGridCards.SelectedItem;
cardName = (dataGridCards.SelectedCells[1].Column.GetCellContent(row) as TextBlock).Text;

DataGrid显示两个表的连接。我真的不喜欢这个解决方案,我宁愿不阻止重新排序。如何在没有块重新排序的情况下获得单元格值?我是否需要为DataGrid行定义一些模式?

2 个答案:

答案 0 :(得分:1)

您可以查询列标题以查找当前索引,然后使用:

object row = dataGridCards.SelectedItem;
int columnIndex = dataGridCards.Columns.Single(c => c.Header.Equals("Card Name")).DisplayIndex;
String cardName = (dataGridCards.SelectedCells[columnIndex].Column.GetCellContent(row) as TextBlock).Text;

答案 1 :(得分:0)

我准备了一个类来填充dataGrid

class RowCardSet
{
    private int cardID;
    private String cardName;
    private String setMark;

    public RowCardSet(int cardID, String cardName, String setMark)
    {
        this.cardID = cardID;
        this.cardName = cardName;
        this.setMark = setMark;
    }

    public int CardID
    {
        get { return this.cardID; }
        set { this.cardID = value; }
    }

    public String CardName
    {
        get { return this.cardName; }
        set { this.cardName = value; }
    }

    public String SetMark
    {
        get { return this.setMark; }
        set { this.setMark = value; }
    }
}

在这种情况下,我还需要在dataGrid中更改Binding。从cardIDcardNamesetMarkCardIDCardNameSetMark

<DataGrid.Columns>
  <DataGridTextColumn Header="Card ID" Binding="{Binding CardID}" IsReadOnly="True"/>
  <DataGridTextColumn Header="Card Name" Binding="{Binding CardName}" IsReadOnly="True"/>
  <DataGridTextColumn Header="Set Mark" Binding="{Binding SetMark}" IsReadOnly="True" Width="*"/>
</DataGrid.Columns>

以下是填充dataGrid的查询:

public static List<RowCardSet> GetCardSetList(int setID, DataContext dataContext)
    {
        return (from cardItem in dataContext.Cards
                join setItem in dataContext.Sets
                on cardItem.setID equals setItem.setID
                where cardItem.setID == setID
                select new RowCardSet(
                    cardItem.cardID,
                    cardItem.cardName,
                    setItem.setMark)).ToList();
    }

所以现在我可以使用这种方法填充:dataGridCards.ItemsSource = Queries.GetCardSetList(setID, dataContext);

获取特定值如下所示:

String cardName = ((RowCardSet)dataGridCards.SelectedItem).CardName.ToString();