У меня есть датагрид, связанный с базой данных. У меня проблема с refre sh данными. У меня есть 7 столбцов, и я использую событие DataGridCellEditEndingEventArgs для столбца индекса = 1-5. Столбец с индексом 0 имеет флажок, а столбец с индексом 6 - кнопку.
Кнопка имеет изображение. Какие шоу добавить / обновить / обновить. Мне нужно обновить sh изображение в кнопке, после события DataGridCellEditEndingEventArgs и когда я нажимаю на кнопку. Но проблема в том, что когда я обновляю sh всю сетку данных, я удаляю добавленные строки или отмену изменений. Но когда я не переосмысливаю sh, я только переопределяю sh источник кнопки, когда нажимаю на эту кнопку.
Можно ли переосмыслить sh один столбец? или другое решение для моей проблемы?
Код:
public partial class MainWindow : Window
{
List<StatusWords> words = new List<StatusWords>();
public MainWindow()
{
InitializeComponent();
WordsDataGrid.Columns.Add(new DataGridTextColumn() { Header = "Kategoria", Binding = new Binding("Category"), Width = new DataGridLength(1, DataGridLengthUnitType.Star) });
WordsDataGrid.Columns.Add(new DataGridTextColumn() { Header = "Mnemonik", Binding = new Binding("Mnemoniese"), Width = new DataGridLength(1, DataGridLengthUnitType.Star) });
words.Add(new StatusWords() { Category = "A", Mnemoniese = "AA" });
words.Add(new StatusWords() { Category = "B", Mnemoniese = "BB" });
words.Add(new StatusWords() { Category = "B", Mnemoniese = "B" });
WordsDataGrid.ItemsSource = words;
}
private void Update_Click(object sender, RoutedEventArgs e)
{
var a = sender as System.Windows.Controls.Button;
if (WordsDataGrid.SelectedIndex > WordsDataGrid.Items.Count - 2)
return;
var index = WordsDataGrid.SelectedIndex;
var itemsSource = WordsDataGrid.ItemsSource as IList<StatusWords>;
itemsSource[index].ImageSource = new Uri(@"/Resources/update.png", UriKind.Relative);
var itemsSource2 = WordsDataGrid.ItemsSource as IList<StatusWords>;
words[WordsDataGrid.SelectedIndex].ImageSource = new Uri(@"\Resources\update.png", UriKind.Relative);
var word = words[WordsDataGrid.SelectedIndex] as StatusWords;
}
private void WordsDataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
int count = 0;
var column = e.Column.DisplayIndex;
if (column < 1 || column > 5)
return;
var row = e.Row.GetIndex();
var newText = ((System.Windows.Controls.TextBox)e.EditingElement).Text;
if (column == 1)
{
words[row].Category = newText;
}
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 (column == 2)
{
words[row].Mnemoniese = newText;
}
words[row].ImageSource = new Uri(@"\Resources\toupdate.png", UriKind.Relative);
//RefreshDataGridWords();
}
private void RefreshDataGridWords()
{
WordsDataGrid.ItemsSource = null;
WordsDataGrid.ItemsSource = words;
}
}
public class StatusWords
{
public bool Status { get; set; }
public Uri ImageSource { get; set; } = new Uri(@"\Resources\update.png", UriKind.Relative);
public string Category { get; set; }
public string Mnemoniese { 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>