C# 使用Caliburn.Micro在MVVM WPF中绑定
这是我的模型:C# 使用Caliburn.Micro在MVVM WPF中绑定,c#,wpf,xaml,binding,C#,Wpf,Xaml,Binding,这是我的模型: public class ValidatedExaminationAnswerModel : ExaminationAnswerModel, INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; public bool IsCorrect { get; set; } = false; } public class O
public class ValidatedExaminationAnswerModel : ExaminationAnswerModel, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public bool IsCorrect { get; set; } = false;
}
public class ObservableExaminationSettingModel
{
public string MaxReactionTime { get; set; }
public bool IsMaxReactionTimeCorrect { get; set; } = false;
public ObservableCollection<ValidatedExaminationAnswerModel> CorrectAnswers { get; set; } = new ObservableCollection<ValidatedExaminationAnswerModel>();
}
public Dictionary<string, ObservableExaminationSettingModel> CasesAnswers { get; set; }
公共类ValidatedExaminationalAnswerModel:ExaminationalAnswerModel,INotifyPropertyChanged
{
公共事件属性更改事件处理程序属性更改;
公共bool IsCorrect{get;set;}=false;
}
公共类ObservieExamicationSettingModel
{
公共字符串MaxReactionTime{get;set;}
公共bool IsMaxReactionTimeCorrect{get;set;}=false;
public ObservableCollection CorrectAnswers{get;set;}=new ObservableCollection();
}
公共字典CasesAnswers{get;set;}
这是我的Xaml:
<ListView.View>
<GridView>
<GridViewColumn
Header="{x:Static localization:Resources.TestCaseNameLable}"
DisplayMemberBinding="{Binding Key}" />
<GridViewColumn Header="Reaction Time">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Grid.Column="1" >
<TextBox.Text>
<Binding Path="Value.MaxReactionTime"
UpdateSourceTrigger="PropertyChanged"/>
</TextBox.Text>
<TextBox.Style>
<Style TargetType="TextBox">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsMaxReactionTimeCorrect, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Value="True">
<Setter Property="Background" Value="White"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=IsMaxReactionTimeCorrect, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Value="False">
<Setter Property="Background" Value="IndianRed"/>
</DataTrigger>
</Style.Triggers>
<Setter Property="cal:Message.Attach" Value="[Event KeyUp] = [Action IsReactionTimeCorrect($dataContext)]"/>
</Style>
</TextBox.Style>
</TextBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
我想将IsMaxReactionTimeCorrect属性绑定到style,以显示值不正确但什么也没有发生,似乎xaml只是没有看到这个变量。我不知道为什么。(当IsMaxReactionTimeCorrect根据我的规则更改其值时)您应该为您的
IsMaxReactionTimeCorrect
实现一个属性Changed
。如果没有,视图不知道您的属性是否更改了其值
public class ObservableExaminationSettingModel : INotifyPropertyChanged
{
public string MaxReactionTime { get; set; }
private bool _isMaxReactionTimeCorrect ;
public bool IsMaxReactionTimeCorrect
{
get
{
return _selected;
}
set
{
_selected = value;
RaisePropertyChanged("IsMaxReactionTimeCorrect");
}
}
public ObservableCollection<ValidatedExaminationAnswerModel> CorrectAnswers { get; set; } = new ObservableCollection<ValidatedExaminationAnswerModel>();
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void RaisePropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
由于您使用的是Fody,
observeExaminationSettingModel
必须从INotifyPropertyChanged
派生。(来自Fody的文档:“所有实现INotifyPropertyChanged的类都将向属性设置器中注入通知代码。”)
public class ObservableExaminationSettingModel : INotifyPropertyChanged
{
}