0

I have a problem with Bindings in WPF(c#).

This is my xaml:

<my:ComboColumn Header="{Binding PropHeader}"
                 DataMemberBinding="{Binding Prop}"
                 ItemsSource="{Binding PossibleProps}"
                 Width="*"
                 />

If I use this binding:

Binding binding = new Binding()
            {
                Path = new PropertyPath(nameof(ItemsSource)),
                Source = this
            };

Then the binding path is correct (is looking for PossibleProps), but is looking at the false Source. Is binding to property PossibleProps in the ViewModel.

If I use this binding:

Binding binding = new Binding()
            {
                Path = new PropertyPath(nameof(ItemsSource)),
                Source = DataContext
            };

Then the binding path is false(is looking for ItemsSource), but is looking at the source I want. Is binding to property ItemsSource in the MainViewModel.

Can you help me please?

update1:

full code:

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;

namespace DB_Admin_WPF.Controls
{
    public partial class ComboColumn : GridViewDataColumn
    {

        public static DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(IEnumerable<KeyValuePair<Guid?, string>>), typeof(ComboColumn), new PropertyMetadata());
        public IEnumerable<KeyValuePair<Guid?, string>> ItemsSource
        {
            get { return (IEnumerable<KeyValuePair<Guid?, string>>)GetValue(ItemsSourceProperty); }
            set { SetValue(ItemsSourceProperty, value); }
        }

        public override FrameworkElement CreateCellElement(GridViewCell cell, object dataItem)
        {
            var element = new TextBlock();
            var valueBinding = new Binding(DataMemberBinding.Path.Path)
            {
                Mode = BindingMode.TwoWay,
                Converter = new ConverterKeyValue() 
            };
            element.SetBinding(TextBlock.TextProperty, valueBinding);

            return element;
        }



        public override FrameworkElement CreateCellEditElement(GridViewCell cell, object dataItem)
        {

            Binding binding = new Binding()
            {
                Path = new PropertyPath(nameof(ItemsSource)),
                Source = DataContext
            };

            var element = new RadComboBox();
            element.SetBinding(RadComboBox.SelectedItemProperty, DataMemberBinding);
            element.DisplayMemberPath = "Value";
            element.SetBinding(RadComboBox.ItemsSourceProperty, binding);
            return element;
        }

        //public IEnumerable<KeyValuePair<Guid?, string>> ItemSource { get; set; }


        public ComboColumn()
        {
            InitializeComponent();
        }

    }
}

Update2

So I did share now the complete code. The xaml is how I want to use my class ComboColumn

The DataContext refers to the MainViewModel while this refers to a ViewModel, which is in a list in the ´MainViewModel´.

In the binding, I want to refer to PossibleProps at the MainViewModel.

7
  • 1
    its not clear what you're trying to do here. You have created a binding in your xaml, why are you then creating bindings in your code? Commented Oct 2, 2020 at 14:20
  • @TimRutter Sry for my bad explanation. I am not that comfortable writing in English. I hope you get my point after the edit. Commented Oct 2, 2020 at 14:36
  • @peni4142: What is correct and what is not? What property are you trying to bind to and from where? Where is PossibleProps and how are you supposed to bind the ItemsSource property to this one? Commented Oct 2, 2020 at 14:42
  • @mm8 I try to bind PossibleProps at the MainViewModel Commented Oct 2, 2020 at 15:07
  • @peni4142: From where? The XAML markup? Commented Oct 2, 2020 at 15:07

1 Answer 1

1

Your ItemsSource="{Binding PossibleProps}" binding will always fail since a DataGridColumn doesn't inherit any DataContext to bind to.

Binding (or DataMemberBinding) is a Binding type property that is applied to the generated element at runtime. It's not resolved from the column.

You can take a look at this answer for an example of how use a Freezable to be able to bind the Visibility property of a column to a source property.

Sign up to request clarification or add additional context in comments.

2 Comments

When I set the Source of the binding to DataContext, I get that output: 'ItemsSource' property not found on 'object' ''MainViewModel' So I think there is a DataContext.
No. When you set the Source explicitly, you don't bind to any inherited DataContext.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.