0
votes

Situation: I have a MainView.xaml and MainViewModel.cs The view is connected to the viewmodel using caliburn micro. The binding is working, but not for that special case: If i initialize my property of type bool in my constructor to false the binding wont update to true in any case. If i initialize the prop to true there is no problem of changing it to false or true later!

The xaml file:

<Style x:Key="EyeXGazeAwareElement" TargetType="FrameworkElement">
        <Setter Property="eyeX:Behavior.GazeAware" Value="{Binding IsGazeActivated}" />
        <Setter Property="eyeX:Behavior.GazeAwareDelay" Value="10" />

...

<Grid  HorizontalAlignment="Center" Style="{StaticResource EyeXGazeAwareElement}" Width="250"></Grid>

And in the ViewModel:

 public bool IsGazeActivated { get; set; }

 public MainViewModel()
    {
        IsGazeActivated = false;
    }


    private void GazeActivatedChanged()
    {
        //this value gets changed, but not in the xaml file...
        //but only if the initial value was set to false, otherwise it is working perfect
        IsGazeActivated = Setting.Instance.IsGazeActivated;
    }

I already tried to use mode=twoway, changed updatesourcetrigger,... but nothing worked!

EDIT: I use PropertyChanged.Fody for weaving my properties. So there is no need to call PropertyChanged manually. The Value of eyeX:Behavior.GazeAware can ether be "True" or "False" and that should map with an bool.. and it is mapping already but not when i initialize the IsGazeActivated to false in the constructor.

2

2 Answers

1
votes

WPF doesn't know what a bool is. You either have to make it a dependency property, or in your case, your VM has to implement INotifyPropertyChanged and raise an event for "IsGazeActivated" when the value changes. Typically the pattern is:

public bool IsGazeActivated 
{
 get { return _bIsGazeActivated; }
 set { if (value != _bIsGazeActivated) RaiseOnPropertyChanged("IsGazeActivated"); }
}
0
votes

You no need to initialize IsGazeActivated inside constructor because its default value is False.
Xaml doesn't update the Normal properties. when ever u applied INotifyPropertyChanged then only its updates.

private bool isGazeActivated;
public bool IsGazeActivated
{
 get{ return isGazeActivated;}
 set
 {
  isGazeActivated = value;
  NotifyPropertyChanged("IsGazeActivated"); or RaiseOnPropertyChanged("IsGazeActivated");
 }
}


Now, its Automatically updates the value form UI.
This will be helpful for your problem.