简体   繁体   中英

Binding in MVVM WPF with Caliburn.Micro

Here is my model:

 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; }

Here is my 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>

I want to bind IsMaxReactionTimeCorrect property to style to show when value is incorrect but nothing happens, seems like xaml just don't see this variable. And I can't figure out why. (When IsMaxReactionTimeCorrect change its value according to my rules)

You should implement an PropertyChanged for your IsMaxReactionTimeCorrect . If not, the View doesn't know if your property changed his value.

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));
        }
    }

If you use Fody for the injection of PropertyChanged event, your model must implement INotifyPropertyChanged

public class ObservableExaminationSettingModel : INotifyPropertyChanged
{
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM