WPF MultiBinding失败。 为什么?
我有这个标记:
在后面的代码中我在window_loaded方法中有这一行:
DataContext = uiManager;
uiManager的类型为UIManager,它有两个名为IsConnected和IsLoggedIn的公共属性。
此代码在启动时失败,因为Multibinding调用的Converter中的values数组未填充布尔值,但值为DependencyProperty.UnsetValue。
当我使用下面的标记(并更改转换器的返回类型)时,它确实有效。
在第一个示例中,似乎通过后面代码中的DataContext的绑定集失败,但在第二个示例中工作。 为什么?
为了UIManager类下面的完整性:
public class UIManager:IUIManager { #region Implementation of IUIManager private const string IsLoggedInProperty = "IsLoggedIn"; private bool loggedIn; private readonly object loggedInLock = new object(); public bool IsLoggedIn { get { lock (loggedInLock) { return loggedIn; } } set { lock (loggedInLock) { if(value==loggedIn)return; loggedIn = value; OnPropertyChanged(IsLoggedInProperty); } } } private void OnPropertyChanged(string property) { if(PropertyChanged!=null)PropertyChanged(this,new PropertyChangedEventArgs(property)); } private const string IsConnectedProperty = "IsConnected"; private bool isConnected; private object isConnectedLock = new object(); public bool IsConnected { get { lock (isConnectedLock) { return isConnected; } } set { lock (isConnectedLock) { if(value==isConnected)return; isConnected = value; OnPropertyChanged(IsConnectedProperty); } } } #endregion #region Implementation of INotifyPropertyChanged public event PropertyChangedEventHandler PropertyChanged; #endregion }
编辑:失败的XAML的转换方法(它在转换为bool值[0]时失败:
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { var is_connected = (bool) values[0]; var is_loggedin = (bool) values[1]; return is_loggedin ? is_connected ? Colors.YellowGreen : Colors.Red : Colors.Gray; }
对于工作XAML:
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { var is_connected = (bool) values[0]; var is_loggedin = (bool) values[1]; return is_loggedin ? is_connected ? Brushes.YellowGreen : Brushes.Red : Brushes.Gray; }
该问题与MultiBinding
或转换器无关。 DependencyProperty.UnsetValue
表示绑定没有值。 事实上,如果您在调试模式下运行,您可以在“ Output
窗口中看到绑定错误:
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=IsConnected; DataItem=null; target element is 'SolidColorBrush' (HashCode=17654054); target property is 'Color' (type 'Color') System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=IsLoggedIn; DataItem=null; target element is 'SolidColorBrush' (HashCode=17654054); target property is 'Color' (type 'Color')
所以让我们稍微简化标记并应用一些诊断:
应用附加的依赖项属性PresentationTraceSources.TraceLevel
产生更多输出:
System.Windows.Data Warning: 52 : Created BindingExpression (hash=17654054) for Binding (hash=44624228) System.Windows.Data Warning: 54 : Path: 'GroupColor' System.Windows.Data Warning: 56 : BindingExpression (hash=17654054): Default mode resolved to OneWay System.Windows.Data Warning: 57 : BindingExpression (hash=17654054): Default update trigger resolved to PropertyChanged System.Windows.Data Warning: 58 : BindingExpression (hash=17654054): Attach to System.Windows.Media.SolidColorBrush.Color (hash=52727599) System.Windows.Data Warning: 60 : BindingExpression (hash=17654054): Use Framework mentor System.Windows.Data Warning: 63 : BindingExpression (hash=17654054): Resolving source System.Windows.Data Warning: 65 : BindingExpression (hash=17654054): Framework mentor not found System.Windows.Data Warning: 61 : BindingExpression (hash=17654054): Resolve source deferred System.Windows.Data Warning: 91 : BindingExpression (hash=17654054): Got InheritanceContextChanged event from SolidColorBrush (hash=52727599) System.Windows.Data Warning: 63 : BindingExpression (hash=17654054): Resolving source System.Windows.Data Warning: 66 : BindingExpression (hash=17654054): Found data context element: GroupBox (hash=51393439) (OK) System.Windows.Data Warning: 67 : BindingExpression (hash=17654054): DataContext is null System.Windows.Data Warning: 91 : BindingExpression (hash=17654054): Got InheritanceContextChanged event from SolidColorBrush (hash=52727599) System.Windows.Data Warning: 63 : BindingExpression (hash=17654054): Resolving source System.Windows.Data Warning: 65 : BindingExpression (hash=17654054): Framework mentor not found System.Windows.Data Warning: 63 : BindingExpression (hash=17654054): Resolving source System.Windows.Data Warning: 65 : BindingExpression (hash=17654054): Framework mentor not found System.Windows.Data Warning: 63 : BindingExpression (hash=17654054): Resolving source (last chance) System.Windows.Data Warning: 65 : BindingExpression (hash=17654054): Framework mentor not found System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=GroupColor; DataItem=null; target element is 'SolidColorBrush' (HashCode=52727599); target property is 'Color' (type 'Color')
我们看到绑定没有找到DataContext
,绑定失败。 当我更改windows的构造函数以便在初始化内容之前设置DataContext
,绑定有效:
public Window1() { DataContext = ...; InitializeComponent(); }
这很奇怪,因为在其他地方绑定并不重要。 不知道为什么它不起作用所以我只能提供解决方法。 例如,使用绑定创建画笔作为资源(该资源也可以是GroupBox
本地资源):
我建议如果您的UIManager
类是某种MVVM
ViewModel
,则删除MultiBinding
并在DataContext
进行一些预处理。
我的理论。 颜色是struct(不能为null),因此SolidColorBrush.Color = null是错误的。 WPF无法创建SolidColorBrush,并且您将获得exception。
BorderBrush是对象(可以为null),因此GroupBox.BorderBrush = null是正常的。
此SolidColorBrush不是对象,而是FACTORY。 它仅在需要时进行实例化,此时您已经附加了DataContext。
只需2美分。
阅读我的文章,顺便说一句,如果您需要一些奇怪的Bindings或Animations与奇怪的转换器,可能会有用。 http://www.codeproject.com/KB/WPF/BindingHub.aspx
出于这样的原因,您可能需要考虑学习MVVM。 这种模式可以帮助您抽象模型和绑定,这样您就不必非常依赖DP – 您可以简单地绑定到视图模型中的可通知属性。
关于MVVM有几篇优秀的文章,所以我建议你先阅读Karl Shifflett,Josh Smith,Marlon Grech和Sacha Barber的作品。