0

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!

Cubemaster
  • 507
  • 1
  • 6
  • 20
  • 1
    I ahve seen a similar question recently: https://stackoverflow.com/questions/51107133/update-multibinding-on-datagridcell – ASh Jul 24 '18 at 16:03
  • The solutions there are very specific to the question asked, I have tried following it to no avail. – Cubemaster Jul 24 '18 at 18:47

1 Answers1

0

On Form1.cs [Design] you could right-click the data grid view of interest and choose 'Edit-Columns'. Then selected the column you are trying to make red and in the 'Appearance' section in the 'Unbound Column Properties' there is a section for DefaultCellStyle, go to that and click on it's [...] button. Now in the open tab change the ForColor to red. This way automatically this column will have red text. When you click the save button the text in the data grid view then can change to black if you add:

       DataGridView.Columns["Value"].DefaultCellStyle.ForeColor = Color.Black;

to the button click method.

G. LC
  • 794
  • 1
  • 8
  • 27
  • This does not work for me. The syntax does not even appear to be correct – Cubemaster Jul 25 '18 at 13:03
  • Sorry it didn't help, were you able to follow the instructions above the code at all or no? – G. LC Jul 25 '18 at 13:12
  • Unfortunately, no. I am not sure if you are using a different version of Visual Studio than I am (or if you are using a different IDE entirely), but I am seeing none of these options. – Cubemaster Jul 25 '18 at 13:15