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