0

I have a tab control that is created from a list of objects and a WPF User control that I want to associate directly with one of these objects. I am creating this association as an Dependency Property in the control.

The binding error I get is:

System.Windows.Data Error: 1 : Cannot create default converter to perform 'one-way' conversions between types 'WPFTester.Control' and 'WPFTester.TestClass'. Consider using Converter property of Binding. BindingExpression:Path=; DataItem='Control' (Name=''); target element is 'Control' (Name=''); target property is 'ClassDependency' (type 'TestClass')

It seems as though it changes its mind what the datacontext is before it binds.

The Main Window Creates the list of Objects and attaches them to a tab control. The Issue I am having regards the binding to the Control Dependency Object here.

XAML:

<Grid>
    <TabControl ItemsSource="{Binding Classes}">
        <TabControl.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Title}"/>
            </DataTemplate>
        </TabControl.ItemTemplate>
        <TabControl.ContentTemplate>
            <DataTemplate>
                <local:Control ClassDependency="{Binding}"/>
            </DataTemplate>
        </TabControl.ContentTemplate>
    </TabControl>
</Grid>

Code-Behind:

public partial class MainWindow : Window, INotifyPropertyChanged
{
    private BindingList<TestClass> classes;

    public event PropertyChangedEventHandler PropertyChanged;

    public BindingList<TestClass> Classes
    {
        get { return classes; }
        set
        {
            classes = value;
            PropertyChanged?.Invoke(this, new 
            PropertyChangedEventArgs("Classes"));
        }
    }

    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;

        Classes = new BindingList<TestClass>
        {
            new TestClass() { Title = "123", Content="abc" },
            new TestClass() { Title = "456", Content="def" }
        };
    }

The rest of the classes are simple and I don't think the issue is in them but I will provide them anyways.

The Data Class is simple:

    public class TestClass
{
    public string Title { get; set; }
    public string Content { get; set; }
}

The User control displays a bound value and a constant value. It also presents a Dependency Property.

XAML:

<StackPanel>
    <TextBlock Text="{Binding TextDependency}" Margin="5"/>
    <TextBlock Text="I'm here"/>
</StackPanel>

Code-Behind:

public partial class Control : UserControl
{
    public static readonly DependencyProperty ClassDependencyProperty = 
        DependencyProperty.RegisterAttached(
        "ClassDependency",
        typeof(TestClass),
        typeof(Control));

    public TestClass ClassDependency
    {
        get { return (TestClass)GetValue(ClassDependencyProperty); }
        set { SetValue(ClassDependencyProperty, value); }
    }

    public Control()
    {
        InitializeComponent();
        this.DataContext = this;
    }
}
etberg
  • 111
  • 1
  • 7
  • Try specifying the type which your `DataTemplate` represents. Like this: `...`. – SharpShade Jan 08 '18 at 22:29
  • `this.DataContext = this;` in the Control constructor prevents that `ClassDependency="{Binding}"` works as you expect. – Clemens Jan 09 '18 at 07:56
  • You have an error: bind to TextDependency and not to ClassDependency – Rekshino Jan 09 '18 at 08:21
  • Thank you Clemens, that was the issue. Here is the full answer https://stackoverflow.com/questions/25672037/how-to-correctly-bind-to-a-dependency-property-of-a-usercontrol-in-a-mvvm-framew?noredirect=1&lq=1 – etberg Jan 09 '18 at 15:59

0 Answers0