我将datagrid绑定到实现INotifyPropertyChanged的项的ObservableCollection。 只有一列是可编辑的。另一列是我希望根据列中编辑的值自动更改的图像。它不起作用。如果我单击行标题(例如,排序),但在编辑
之后不立即刷新<DataGrid ItemsSource="{Binding ItemRows,UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" AutoGenerateColumns="False"
CanUserAddRows="False" CanUserDeleteRows="False" CanUserReorderColumns="false">
<DataGrid.Columns>
<DataGridTextColumn Header="Editable" Binding="{Binding Editable, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" />
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image x:Name="imgComplete" Width="20" Height="20">
<Image.Style>
<Style TargetType="{x:Type Image}">
<Setter Property="Source" Value="{StaticResource imgCheckmarkOrange}" />
<Style.Triggers>
<DataTrigger Value="0" Binding="{Binding Editable,UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">
<Setter Property="Source" Value="{StaticResource imgSadFace}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
编辑:添加我的View模型的部分内容
我正在使用FODY,我的结构非常复杂,所以我只发布与datagrid源相关的内容
[ImplementPropertyChanged]
public class MyViewModel
{
....
public ObservableCollection<StockOrderRow> ItemRows { get; set; } = new ObservableCollection<StockOrderRow>();
.....
}
此集合在更改另一个属性时从数据库加载数据
班级“StockOrderRow”
public class StockOrderRow : BaseEntity
{
public string Field1{ get; set; } = "";
...
public int Editable { get; set; } = 0;
}
班级“BaseEntity”
public class BaseEntity: INotifyPropertyChanged
{
public string Id { get; set; }
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
答案 0 :(得分:1)
我建议您使用DataGridTemplateColumn作为您的Editable属性。有了这个,您可以直接在TextBox中处理与PropertyChanged的绑定。
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Editable}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox Text="{Binding Editable, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
第二件事是你的ItemsClasses属性Editable必须调用OnPropertyChange
public class StockOrderRow:BaseEntity
{
public string Field1{ get; set; } = "";
private int _editable = 0;
public int Editable {
get { return _editable; }
set {
if(value == _editable) return;
// Debug.WriteLine("Editable set. OldVal: " + _editable + "; NewVal:" + value);
_editable = value;
OnPropertyChanged(nameof(Editable));
}
}
}
您的DataTrigger无需在模式TwoWay中绑定
<Style.Triggers>
<DataTrigger Value="0" Binding="{Binding Editable,UpdateSourceTrigger=PropertyChanged}">
<Setter Property="Source" Value="{StaticResource imgSadFace}"/>
</DataTrigger>
</Style.Triggers>
如果这不起作用,我将在可编辑的设置器处开始调试(并检查,如果在TextBox中键入时属性已更新...)。