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.

peni4142
  • 426
  • 1
  • 7
  • 26
  • 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? – Tim Rutter Oct 02 '20 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. – peni4142 Oct 02 '20 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? – mm8 Oct 02 '20 at 14:42
  • @mm8 I try to bind `PossibleProps` at the `MainViewModel` – peni4142 Oct 02 '20 at 15:07
  • @peni4142: From where? The XAML markup? – mm8 Oct 02 '20 at 15:07
  • @peni4142 your english is perfectly fine :) – Tim Rutter Oct 02 '20 at 15:09
  • @mm8 Yeah the XAML markup is how I would like to make use of the class ComboColumn. The element `my:ComboColumn` is a child of `telerik:RadGridView.Columns`. – peni4142 Oct 02 '20 at 15:17

1 Answers1

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.

mm8
  • 163,881
  • 10
  • 57
  • 88
  • 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. – peni4142 Oct 02 '20 at 15:31
  • No. When you set the `Source` explicitly, you don't bind to any inherited `DataContext`. – mm8 Oct 05 '20 at 13:54