Wpf ViewModel中的属性更改时,未调用ValueConverter
我需要根据ViewModel中的布尔属性将窗口的光标更改为沙漏,为此,我定义了一个转换器,可将布尔转换为光标,如下所示:Wpf ViewModel中的属性更改时,未调用ValueConverter,wpf,Wpf,我需要根据ViewModel中的布尔属性将窗口的光标更改为沙漏,为此,我定义了一个转换器,可将布尔转换为光标,如下所示: [ValueConversion(typeof(bool), typeof(Cursors))] public class CursorExtensionConverter : IValueConverter { public object Convert(object value, Type targetType, object paramet
[ValueConversion(typeof(bool), typeof(Cursors))]
public class CursorExtensionConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value != null && ((bool)value))
{
return Cursors.Wait;
}
return Cursors.Arrow;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
在XAML中,我使用以下标记将Window.Cursor绑定到转换器
<Window.Resources>
<Converters:CursorExtensionConverter x:Key="cursorExtensionConverter"/>
</Window.Resources>
<Window.Cursor>
<Binding Path="IsBusy" Converter="{StaticResource cursorExtensionConverter}"/>
</Window.Cursor>
在ViewModel中,当我设置IsBusy
=true时;未调用CursorExtensionConverter
中的Convert
函数。为什么?
谢谢问题是由于窗口的DataContext设置为ViewModel和解析XAML的顺序造成的。此外,它还与ViewModel实现中如何处理PropertyChanged事件有关。我无法粘贴到这里 我还没有完全弄清楚,但在这种特殊情况下,在代码隐藏中构建绑定解决了这个问题:
Binding binding = new Binding();
binding.Source = viewmodel;
binding.Path = new PropertyPath("IsBusy");
binding.Converter = new CursorExtensionConverter();
SetBinding(CursorProperty, binding);
绑定错误?Implemented INotifyPropertyChanged?HB可能是对的,但是您应该显示您的ViewModel,因为您显示了其余部分。@H.B.我想问题是绑定错误,但不知道在哪里。是,ViewModel实现INotifyPropertyChanged。@sean717: