Reputation: 545
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?
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();
}
}
}
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
.
Upvotes: 0
Views: 291
Reputation: 169150
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.
Upvotes: 1