C# 是否可以只刷新数据网格中的一列?wpf

C# 是否可以只刷新数据网格中的一列?wpf,c#,wpf,datagrid,C#,Wpf,Datagrid,我将datagrid与数据库连接。我有刷新数据的问题。 我有7列,我使用事件DataGridCellEditEndingEventArgs作为列索引=1-5。 索引为0的列具有复选框,索引为6的列具有按钮 按钮有图像。显示add/update/toupdate。我需要在事件DataGridCellEditEndingEventArgs之后以及单击按钮时刷新按钮中的图像。但问题是,当我刷新整个datagrid时,我会删除添加的行或撤消更改。但当我不刷新它时,我只会在单击此按钮时刷新按钮源 是否可以

我将datagrid与数据库连接。我有刷新数据的问题。 我有7列,我使用事件DataGridCellEditEndingEventArgs作为列索引=1-5。 索引为0的列具有复选框,索引为6的列具有按钮

按钮有图像。显示add/update/toupdate。我需要在事件DataGridCellEditEndingEventArgs之后以及单击按钮时刷新按钮中的图像。但问题是,当我刷新整个datagrid时,我会删除添加的行或撤消更改。但当我不刷新它时,我只会在单击此按钮时刷新按钮源

是否可以刷新一列?还是其他解决我问题的方法

代码:

公共部分类主窗口:窗口
{
列表单词=新列表();
公共主窗口()
{
初始化组件();
WordsDataGrid.Columns.Add(newdatagridtextcolumn(){Header=“Kategoria”,Binding=newbinding(“Category”),Width=newdatagridlength(1,datagridlengthunitype.Star)});
添加(新DataGridTextColumn(){Header=“Mnemonik”,Binding=newbinding(“Mnemoniese”),Width=newDataGridLength(1,DataGridLengthunitype.Star)});
添加(新的StatusWords(){Category=“A”,Mnemoniese=“AA”});
添加(新的StatusWords(){Category=“B”,Mnemoniese=“BB”});
添加(新的StatusWords(){Category=“B”,Mnemoniese=“B”});
WordsDataGrid.ItemsSource=单词;
}
私有无效更新\u单击(对象发送者,路由目标e)
{
var a=发送方为System.Windows.Controls.Button;
如果(WordsDataGrid.SelectedIndex>WordsDataGrid.Items.Count-2)
返回;
var index=WordsDataGrid.SelectedIndex;
var itemsource=WordsDataGrid.itemsource作为IList;
itemsSource[index].ImageSource=newURI(@“/Resources/update.png”,UriKind.Relative);
var itemsSource2=WordsDataGrid.ItemsSource作为IList;
words[WordsDataGrid.SelectedIndex].ImageSource=newURI(@“\Resources\update.png”,UriKind.Relative);
var word=words[WordsDataGrid.SelectedIndex]作为状态词;
}
私有void WordsDataGrid_CellEditEnding(对象发送方,DataGridCellEditEndingEventArgs e)
{
整数计数=0;
var column=e.column.DisplayIndex;
如果(列<1 | |列>5)
返回;
var row=e.row.GetIndex();
var newText=((System.Windows.Controls.TextBox)e.EditingElement).Text;
如果(列==1)
{
单词[行]。类别=新文本;
}
if(column==2&&words[row].Mnemoniese!=null&&words.Where(x=>x.Mnemoniese!=null&&x.Mnemoniese.ToLower()==newText.ToLower()).Count()>Count&&words[row].Mnemoniese.ToLower()!=newText.ToLower())
{
MessageBox.Show(“Name”+newText+“exist”);
}
else if(列==2)
{
单词[行]。助记符=新文本;
}
words[row].ImageSource=newURI(@“\Resources\toupdate.png”,UriKind.Relative);
//RefreshDataGridWords();
}
私有void RefreshDataGridWords()
{
WordsDataGrid.ItemsSource=null;
WordsDataGrid.ItemsSource=单词;
}
}
公共类状态词
{
公共布尔状态{get;set;}
公共Uri ImageSource{get;set;}=新Uri(@“\Resources\update.png”,UriKind.Relative);
公共字符串类别{get;set;}
公共字符串助记符{get;set;}
}
和xaml:

<Grid>
    <DataGrid x:Name="WordsDataGrid" Grid.Column="0" Grid.Row="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" 
              AutoGenerateColumns="False" CellEditEnding="WordsDataGrid_CellEditEnding">
        <DataGrid.Columns>
            <DataGridCheckBoxColumn Header="X" Width="10" Binding="{Binding Status}"/>
            <DataGridTemplateColumn >
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Button Visibility="Visible" Height="16" Width="16" Click="Update_Click">
                            <Button.Content>
                                <Image x:Name="KeyName"  Source="{Binding ImageSource}"  />
                            </Button.Content>
                        </Button>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>
</Grid>

定义了
ImageSource
属性的
StatusWords
类必须实现
INotifyPropertyChanged
接口,并在将属性设置为新值时引发该属性的
PropertyChanged
事件:

public class StatusWords : INotifyPropertyChanged
{
    public bool Status { get; set; }

    private Uri _imageSource = new Uri(@"\Resources\update.png", UriKind.Relative);
    public Uri ImageSource
    {
        get { return _imageSource; }
        set { _imageSource = value; NotifyPropertyChanged(); }
    }

    public string Category { get; set; }
    public string Mnemoniese { get; set; }

    public event PropertyChangedEventHandler PropertyChanged; 
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

按照我的建议,通过设置列中显示的数据对象的属性来刷新列。它不起作用。图像没有改变。我添加图像,你可以看。图像是相同的;)恐怕这里没有我可以看的。我补充道。您只需添加Resources\update.png nad toupdate.png。更改文本框中的描述或单击按钮只能刷新最后一列或一个按钮无法刷新整个网格。非常感谢。
public class StatusWords : INotifyPropertyChanged
{
    public bool Status { get; set; }

    private Uri _imageSource = new Uri(@"\Resources\update.png", UriKind.Relative);
    public Uri ImageSource
    {
        get { return _imageSource; }
        set { _imageSource = value; NotifyPropertyChanged(); }
    }

    public string Category { get; set; }
    public string Mnemoniese { get; set; }

    public event PropertyChangedEventHandler PropertyChanged; 
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}