我试图学习和理解c#中的功能区控件,尤其是数据绑定,但是在实现RibbonComboBox时收到一些我无法解决的警告。我设法删除了错误消息,但警告仍然存在。我该如何解决?
相关文件如下所示,我已删除了无关的信息以简化它们。
这是XAML文件- MainWindow.xaml :
<RibbonWindow x:Class="MyProject.MainWindow"
...
xmlns:local="clr-namespace:MyProject"
xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"
mc:Ignorable="d"
Title="MyProject" Height="450" Width="800" x:Name="MyProjectControl" >
<Grid>
<Ribbon DockPanel.Dock="Top" x:Name="Ribbon">
<RibbonTab Header="Home" >
<RibbonGroup Header="Shapes" Width="160">
<RibbonComboBox x:Name="cbShape" Height="Auto" Width="Auto" Label="Shape" VerticalAlignment="Center">
<RibbonGallery x:Name="shapeComboBox" SelectedItem="{Binding Path=Shape, UpdateSourceTrigger=PropertyChanged, Mode=OneWay, diag:PresentationTraceSources.TraceLevel=High}" >
<RibbonGalleryCategory x:Name="shapeList" ItemsSource="{Binding Path=Shape, Mode=OneWay}" />
</RibbonGallery>
</RibbonComboBox>
</RibbonGroup>
</RibbonTab>
</Ribbon>
</Grid>
</RibbonWindow>
其背后的代码- MainWindow.xaml.cs :
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;
}
}
}
RibbonSettings.cs 类:
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'
菜单项正确显示在列表中,我可以从列表中选择一个选项。
我在绑定中添加了“ UpdateSourceTrigger = PropertyChanged”,从而删除了“ System.Windows.Data警告:61:BindingExpression(hash = 35104124):默认更新触发器已解析为PropertyChanged ”错误,但其他人仍然难以捉摸。
答案 0 :(得分:1)
您所指的警告只是扩展的跟踪信息。只要绑定按预期工作,就无需检查它们。但是,如果在输出中看到 System.Windows.Data错误,则意味着您需要注意它的内容(在我的情况下,最常见的原因是属性名称中有错字)。
您可以通过更改 WPF跟踪设置 下的数据绑定设置,在选项->调试->输出窗口中更改TraceLevel for WPF Bindings。 。
这是关于Debugging WPF Bindings的非常不错的教程。