C# 带RibbonComboBox的WPF绑定警告
我试图学习和理解c语言中的ribbon控件,特别是数据绑定,但在实现RibbonComboBox时收到了许多无法解决的警告。我已成功删除错误消息,但警告仍然存在。我怎样才能修好它 相关文件如下所示,我删除了无关信息以简化它们 这是XAML文件-MainWindow.XAML: 调试输出为:C# 带RibbonComboBox的WPF绑定警告,c#,wpf,data-binding,C#,Wpf,Data Binding,我试图学习和理解c语言中的ribbon控件,特别是数据绑定,但在实现RibbonComboBox时收到了许多无法解决的警告。我已成功删除错误消息,但警告仍然存在。我怎样才能修好它 相关文件如下所示,我删除了无关信息以简化它们 这是XAML文件-MainWindow.XAML: 调试输出为: System.Windows.Data Warning: 56 : Created BindingExpression (hash=47501665) for Binding (hash=55144039)
System.Windows.Data Warning: 56 : Created BindingExpression (hash=47501665) for Binding (hash=55144039)
System.Windows.Data Warning: 58 : Path: 'Shape'
System.Windows.Data Warning: 62 : BindingExpression (hash=47501665): Attach to System.Windows.Controls.Ribbon.RibbonGallery.SelectedItem (hash=13583655)
System.Windows.Data Warning: 67 : BindingExpression (hash=47501665): Resolving source
System.Windows.Data Warning: 70 : BindingExpression (hash=47501665): Found data context element: RibbonGallery (hash=13583655) (OK)
System.Windows.Data Warning: 71 : BindingExpression (hash=47501665): DataContext is null
System.Windows.Data Warning: 65 : BindingExpression (hash=47501665): Resolve source deferred
System.Windows.Data Warning: 67 : BindingExpression (hash=47501665): Resolving source
System.Windows.Data Warning: 70 : BindingExpression (hash=47501665): Found data context element: RibbonGallery (hash=13583655) (OK)
System.Windows.Data Warning: 78 : BindingExpression (hash=47501665): Activate with root item RibbonSettings (hash=61356140)
System.Windows.Data Warning: 108 : BindingExpression (hash=47501665): At level 0 - for RibbonSettings.Shape found accessor RuntimePropertyInfo(Shape)
System.Windows.Data Warning: 104 : BindingExpression (hash=47501665): Replace item at level 0 with RibbonSettings (hash=61356140), using accessor RuntimePropertyInfo(Shape)
System.Windows.Data Warning: 101 : BindingExpression (hash=47501665): GetValue at level 0 from RibbonSettings (hash=61356140) using RuntimePropertyInfo(Shape): 'Square'
System.Windows.Data Warning: 80 : BindingExpression (hash=47501665): TransferValue - got raw value 'Square'
System.Windows.Data Warning: 89 : BindingExpression (hash=47501665): TransferValue - using final value 'Square'
菜单项正确显示在列表中,我可以从列表中选择一个选项
我在绑定中添加了'UpdateSourceTrigger=PropertyChanged',该绑定删除了'System.Windows.Data警告:61:BindingExpression hash=35104124:Default update trigger resolved to PropertyChanged'错误,但其他错误仍然无法找到。您提到的警告只是扩展跟踪信息。只要绑定按预期工作,就不需要检查它们。但是,如果您在输出中看到System.Windows.Data错误,这意味着您需要注意它所说的内容。在我的例子中,最常见的原因是属性名称中的键入错误 通过更改WPF跟踪设置下的数据绑定设置,可以在选项->调试->输出窗口中进行更改
这是一个非常好的教程。你是说代码工作正常,但是警告让你很烦恼吗?如果是这样,不要担心警告。顺便说一句,为什么SelectedItem上的绑定是单向的?最初我在SelectedItem和ItemsSource上都将绑定设置为双向,这导致了一个错误。我将两者都更改以删除错误,但现在已将其更改回单向。
using System.Collections.Generic;
using System.Windows.Controls.Ribbon;
namespace MyProject
{
public partial class MainWindow : RibbonWindow
{
public MainWindow()
{
InitializeComponent();
InitializeLists();
this.DataContext = new RibbonSettings();
}
private void InitializeLists()
{
List<string> MyShapes = new List<string>
{
"Square", "Circle", "Ellipse", "Triangle", "Pentagon"
};
shapeList.ItemsSource = MyShapes;
}
}
}
using System.ComponentModel;
namespace MyProject
{
class RibbonSettings : INotifyPropertyChanged
{
private string _shape = "Square";
public string Shape
{
get { return _shape; }
set
{
if (_shape == value) return;
_shape = value;
OnPropertyChanged("Shape");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
System.Windows.Data Warning: 56 : Created BindingExpression (hash=47501665) for Binding (hash=55144039)
System.Windows.Data Warning: 58 : Path: 'Shape'
System.Windows.Data Warning: 62 : BindingExpression (hash=47501665): Attach to System.Windows.Controls.Ribbon.RibbonGallery.SelectedItem (hash=13583655)
System.Windows.Data Warning: 67 : BindingExpression (hash=47501665): Resolving source
System.Windows.Data Warning: 70 : BindingExpression (hash=47501665): Found data context element: RibbonGallery (hash=13583655) (OK)
System.Windows.Data Warning: 71 : BindingExpression (hash=47501665): DataContext is null
System.Windows.Data Warning: 65 : BindingExpression (hash=47501665): Resolve source deferred
System.Windows.Data Warning: 67 : BindingExpression (hash=47501665): Resolving source
System.Windows.Data Warning: 70 : BindingExpression (hash=47501665): Found data context element: RibbonGallery (hash=13583655) (OK)
System.Windows.Data Warning: 78 : BindingExpression (hash=47501665): Activate with root item RibbonSettings (hash=61356140)
System.Windows.Data Warning: 108 : BindingExpression (hash=47501665): At level 0 - for RibbonSettings.Shape found accessor RuntimePropertyInfo(Shape)
System.Windows.Data Warning: 104 : BindingExpression (hash=47501665): Replace item at level 0 with RibbonSettings (hash=61356140), using accessor RuntimePropertyInfo(Shape)
System.Windows.Data Warning: 101 : BindingExpression (hash=47501665): GetValue at level 0 from RibbonSettings (hash=61356140) using RuntimePropertyInfo(Shape): 'Square'
System.Windows.Data Warning: 80 : BindingExpression (hash=47501665): TransferValue - got raw value 'Square'
System.Windows.Data Warning: 89 : BindingExpression (hash=47501665): TransferValue - using final value 'Square'