I have a datagrid in my WPF application, and it has an editable column for the Value
property of my Device
class. However, I have made it so the changes only update when the user clicks a Save button. I want to make the user's changes turn red on the datagrid while they have yet to save the changes, but I cannot figure out how to make this work. Here is the relevant section of my Device
class:
private string _value;
public string Value
{
get { return _value; }
set
{
base.SetProperty(ref this._value, value);
if (value != OriginalValue)
{
isDirty = true;
}
}
}
private string _originalValue;
public string OriginalValue
{
get { return _originalValue; }
set
{
base.SetProperty(ref _originalValue, value);
}
}
public void Commit()
{
this.OriginalValue = this.Value;
}
And here is the handler for the save button:
private void SaveButton_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Changes have been saved to Database");
foreach (Device foo in MasterDataGrid.ItemsSource)
{
foo.Commit();
}
}
There is no attached database, this is just flavor text.
I have tried adding an isDirty
boolean to the device class, but I cannot figure out how to link the value of that boolean to the text color in my DataGrid. Here is the relevant XAML for the DataGrid:
<DataGridTemplateColumn Header="Value">
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<Grid FocusManager.FocusedElement="{Binding ElementName=textBox1}">
<TextBox Name="textBox1" TextChanged="TextBox1_OnTextChanged" GotFocus="TextBox_GotFocus" Margin="0" Padding="-2" MaxHeight="29" Text="{Binding Path=Value, Mode=TwoWay, UpdateSourceTrigger=Explicit}" />
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Value, Mode=TwoWay,UpdateSourceTrigger=Explicit}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
I have tried using the OnTextChanged
handler, but I cannot figure out how to reference the Device
object who's value is being displayed. I feel like I might be close, but there are a few missing links.
If anyone can give me a hand, I would greatly appreciate it. Thanks for the help!