情况: 我有一个MainView.xaml和MainViewModel.cs 视图使用caliburn micro连接到视图模型。 绑定工作正常,但不适用于特殊情况: 如果我在构造函数中将bool类型的属性初始化为false,则绑定在任何情况下都不会更新为true。 如果我将prop初始化为true,那么以后就没有问题可以将它改为false或true!
xaml文件:
<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>
在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;
}
我已经尝试过使用mode = twoway,更改了updatesourcetrigger,......但没有任何效果!
编辑: 我使用PropertyChanged.Fody来编织我的属性。 因此无需手动调用PropertyChanged。 eyeX的值:Behavior.GazeAware可以是“True”或“False”,并且应该使用bool映射..并且它已经映射但不是在构造函数中将IsGazeActivated初始化为false时。
答案 0 :(得分:1)
WPF并不知道bool是什么。您要么必须使其成为依赖属性,要么在您的情况下,您的VM必须实现INotifyPropertyChanged并为&#34; IsGazeActivated&#34;当价值变化时。通常,模式是:
match_all
答案 1 :(得分:0)
您无需在构造函数中初始化 IsGazeActivated ,因为其默认值为 False 。
Xaml不会更新Normal属性。什么时候你应用INotifyPropertyChanged然后只应用它的更新。
private bool isGazeActivated;
public bool IsGazeActivated
{
get{ return isGazeActivated;}
set
{
isGazeActivated = value;
NotifyPropertyChanged("IsGazeActivated"); or RaiseOnPropertyChanged("IsGazeActivated");
}
}
现在,它自动更新值表单UI。
这对您的问题很有帮助。