【发布时间】:2018-07-24 15:51:22
【问题描述】:
我的 WPF 应用程序中有一个数据网格,它有一个用于我的 Device 类的 Value 属性的可编辑列。但是,我已经做到了,因此更改仅在用户单击“保存”按钮时才会更新。我想让用户的更改在数据网格上变成红色,而他们尚未保存更改,但我不知道如何使这项工作。这是我的Device 课程的相关部分:
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;
}
这里是保存按钮的处理程序:
private void SaveButton_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Changes have been saved to Database");
foreach (Device foo in MasterDataGrid.ItemsSource)
{
foo.Commit();
}
}
没有附加数据库,这只是风味文本。
我尝试将isDirty 布尔值添加到设备类,但我不知道如何将该布尔值的值链接到我的DataGrid 中的文本颜色。这是 DataGrid 的相关 XAML:
<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>
我尝试使用OnTextChanged 处理程序,但我不知道如何引用正在显示值的Device 对象。我觉得我可能很接近,但是缺少一些链接。
如果有人能帮我一把,我将不胜感激。感谢您的帮助!
【问题讨论】:
-
我最近看到一个类似的问题:stackoverflow.com/questions/51107133/…
-
那里的解决方案对所提出的问题非常具体,我尝试遵循它无济于事。