Search code examples
c#wpfdata-binding

WPF Binding Warnings with RibbonComboBox


I'm trying to learn and understand the ribbon controls in c#, particularly data binding, but receive a number of warnings I can't resolve when implementing a RibbonComboBox. I've managed to remove the error messages but the warnings persist. How can I fix it?

The relevant files are shown below and I've removed extraneous information to simplify them.

This is the XAML file - 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>

The code behind it - 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;
        }
    }
}

The RibbonSettings.cs class:

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));
            }
        }
    }
}

The debug output is:

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'

The menu items correctly appear in the list and I can select an option from the list.

I added 'UpdateSourceTrigger=PropertyChanged' to the binding which removed a 'System.Windows.Data Warning: 61 : BindingExpression (hash=35104124): Default update trigger resolved to PropertyChanged' error but the others remain elusive.


Solution

  • The warnings you are referring to are just extended trace information. As long as the bindings work as you expect, there is no need to examine those. But if you see System.Windows.Data Error in output, it means you need to pay attention to what it says (in my case the most frequent reason is a typo in property name).

    You can change TraceLevel for WPF Bindings in Options -> Debugging -> Output Window by changing Data Binding setting under WPF Trace Settings.

    Here is a pretty nice tutorial on Debugging WPF Bindings.