我的WPF应用程序中有一个DataGrid,其中包含保存布尔值的列 . 我想镜像DataGridCheckBoxColumn但我想将复选框更改为彩色文本框 . 因此,对于真正的值,它将显示绿色的“通过”,而对于虚假的红色“失败” . 我已经尝试设置DataGridCheckBoxColumn.ElementStyle以及一个带有按钮的DataGridTemplateColumn但绑定不会更新 . 它们在加载时设置正常,但在项目更新时没有设置 . 数据网格包含TestPointAttempts的集合 .
这是模板列的代码,里面有一个按钮 .
<DataGridTemplateColumn Header="Talk Out" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="{Binding TalkOut, Converter={StaticResource BoolToPassFailConverter}, Mode=TwoWay}"
Foreground="{Binding TalkOut, Converter={StaticResource BoolToGreenRedConverter}, Mode=TwoWay}"
Style="{StaticResource TestResultButtonStyle}"
Click="TalkOutButton_Click"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
我能够使点击事件工作并更新代码中的“TalkOut”值,但按钮Text和Foreground不会更新 . 在我的viewmodel中,我有代码来更新项目 . 我将所选单元格作为TestPointAttempt抓取并将其传递给viewmodel进行更新 . 这是我的viewmodel代码:
public void ToggleTestResult(TestPointAttempt tpa, bool inOutSwitch)
{
if (inOutSwitch)
{
tpa.TalkIn = !tpa.TalkIn;
RaisePropertyChanged(() => tpa.TalkIn);
}
else
{
tpa.TalkOut = !tpa.TalkOut;
RaisePropertyChanged(() => tpa.TalkOut);
}
}
这是我的testPointAttempt模型:
public class TestPointAttempt
{
public string Id { get; set; }
public int TestAttemptNumber { get; set; }
public bool? TalkIn { get; set; }
public bool? TalkOut { get; set; }
...
}
如何让单元格更新更新?
1 回答
TestPointAttempt需要实现INotifyPropertyChanged,并且setter需要触发propertyChanged事件 .
另一种方法是使用Fody .