获取绑定到LINQ查询结果的DataGrid中DataGridTemplateColumn的编辑值

时间:2019-03-08 04:11:02

标签: wpf data-binding linq-to-sql wpfdatagrid

我有一个WPF应用程序,它带有一个数据网格,该网格绑定到ViewModel中的自定义LINQ to SQL查询中,该查询将来自两个不同表的列合并在一起:

public ICollectionView SetsView { get; set; }

public void UpdateSetsView()
{
    var sets = (from s in dbContext.Sets
                join o in dbContext.SetParts on s.ID equals o.SetID into g1
                select new
                {
                    s.ID,
                    s.SetNumber,
                    s.SetTitle,
                    s.SetType,
                    s.SetNotes,
                    s.SetUrl,
                    s.HaveAllParts,
                    s.NumberOfSets,
                    s.IsBuilt,
                    s.DateAdded,
                    s.LastModified,
                    UniqueParts = g1.Count(),
                    TotalParts = g1.Sum(o => o.Quantity)
                }
                );
    SetsView = CollectionViewSource.GetDefaultView(sets);
}

SetsView集合绑定到我的数据网格,由于我需要能够编辑任何行的SetNotes值并将其保存回数据库中的Sets表中,因此我为CellEditEnding添加了一个事件处理程序(CellEditEnding =“ dgST_Sets_CellEditEnding ”)添加到DataGrid定义并创建此列:

<DataGridTemplateColumn Header="Set Notes" 
                        SortMemberPath="SetNotes" 
                        Width="*">
    <DataGridTemplateColumn.HeaderStyle>
        <Style TargetType="{x:Type DataGridColumnHeader}">
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
        </Style>
    </DataGridTemplateColumn.HeaderStyle>
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding SetNotes, Mode=OneWay}" Margin="5,0,5,0"
                        HorizontalAlignment="Stretch" VerticalAlignment="Center" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
    <DataGridTemplateColumn.CellEditingTemplate>
        <DataTemplate>
            <TextBox Text="{Binding SetNotes, Mode=OneWay}" Margin="5,0,5,0"
                        HorizontalAlignment="Stretch" VerticalAlignment="Center" 
                        />
        </DataTemplate>
    </DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>

问题是,当我运行应用程序并在任何行中编辑“设置注​​释”列时,我无法弄清楚如何从事件args获取已编辑单元格的新值。我以为可以将事件args EditingElement实例强制转换为TextBox(请参见下面的处理程序),但是当我运行应用程序时,编辑一行并更改SetNotes的值,EditingElement的类型为ContentPresenter而不是TextBox,在我的生命中无法弄清楚如何获得更改后的值。

private void dgST_Sets_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
    string newValue = ((TextBox)e.EditingElement).Text;

    //Code here to update table
}

请记住,我绑定到自定义LINQ to SQL查询,因此这不是经典的模型绑定问题。还要注意,在我的模板列中绑定SetNotes的值时,我别无选择,但使用Mode = OneWay就像使用其他任何选项一样,会给我有关访问只读属性的运行时错误-可能是问题所在吗?

我已经花了几个小时,无休止地搜索谷歌-有人可以帮我吗?

1 个答案:

答案 0 :(得分:1)

这应该有效:

private void dgST_Sets_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
    ContentPresenter cp = e.EditingElement as ContentPresenter;
    if (cp != null && VisualTreeHelper.GetChildrenCount(cp) > 0)
    {
        TextBox textBox = VisualTreeHelper.GetChild(cp, 0) as TextBox;
        if (textBox != null)
        {
            string newValue = textBox.Text;
        }
    }
}