Reputation: 559
I have 2 object types which display on tabs within the same tab. I am using a DataTrigger and a DataTypeConverter to display the content. The content displays correctly but I receive the following errors in the output window:
System.Windows.Data Error: 40 : BindingExpression path error: 'Taste' property not found on 'object' ''MySmellObject' (HashCode=30266853)'. BindingExpression:Path=Taste; DataItem='MySmellObject' (HashCode=30266853); target element is 'TextBox' (Name=''); target property is 'Text' (type 'String')
System.Windows.Data Error: 40 : BindingExpression path error: 'Smell' property not found on 'object' ''MyTasteObject' (HashCode=36404074)'. BindingExpression:Path=Smell; DataItem='MyTasteObject' (HashCode=36404074); target element is 'TextBox' (Name=''); target property is 'Text' (type 'String')
Here's the XAML:
<Window x:Class="ControlTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ControlTest"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:DataTypeConverter x:Key="DataTypeConverter" />
</Window.Resources>
<StackPanel Orientation="Vertical" Width="150">
<TabControl Name="tab" Height="200px"
ItemsSource="{Binding MyObjects}"
SelectedValuePath="Id"
SelectedItem="{Binding MyObject}"
>
<TabControl.ContentTemplate>
<DataTemplate>
<ContentControl>
<ContentControl.Style>
<Style TargetType="ContentControl">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=tab,Path=SelectedItem, Converter={StaticResource DataTypeConverter}}" Value="{x:Type local:MyTasteObject}">
<Setter Property="Content">
<Setter.Value>
<TextBox Text="{Binding Taste}" ></TextBox>
</Setter.Value>
</Setter>
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=tab,Path=SelectedItem, Converter={StaticResource DataTypeConverter}}" Value="{x:Type local:MySmellObject}">
<Setter Property="Content">
<Setter.Value>
<TextBox Text="{Binding Smell}" ></TextBox>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
</StackPanel>
</Window>
Here's the code behind:
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
namespace ControlTest
{
public partial class MainWindow : Window
{
public List<MyObject> MyObjects { get; set; }
public int MyObjectId { get; set; }
public MyObject MyObject { get; set; }
public MainWindow()
{
MyObject obj0 = new MySmellObject() { Id = 0, Smell = "Pleasent" };
MyObject obj1 = new MyTasteObject() { Id = 1, Taste = "Mild" };
MyObjects = new List<MyObject> { obj0, obj1 };
MyObjectId = 0;
MyObject = obj0;
DataContext = this;
InitializeComponent();
}
}
public class MyObject
{
public int Id { get; set; }
}
public class MyTasteObject : MyObject
{
public string Taste { get; set; }
public override string ToString()
{
return Taste;
}
}
public class MySmellObject : MyObject
{
public String Smell { get; set; }
public override string ToString()
{
return Smell;
}
}
public class DataTypeConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter,
CultureInfo culture)
{
return value.GetType();
}
public object ConvertBack(object value, Type targetType, object parameter,
CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
Does anyone know why I'm getting these errors and how to fix the code?
Upvotes: 0
Views: 1319
Reputation: 63317
The problem here is the ContentTemplate
is applied against all items in the TabControl
. Initially it's applied only on the SelectedItem. But after switching to the new item, it's then applied to all loaded items.
When the SelectedItem is changed, the ContentTemplate
with a ContentControl having new Content
updated, this is then applied on all items. But because the underlying items are of different concrete types (deriving from the same base type). So once the template is applied on item of mismatched type, the Binding error will be reported silently.
In fact we have a better way to achieve what you want without using Triggers at all. That's what the so-called DataTemplate
provides you. Just set the DataType
to the item type, then it will be automatically replaced with the corresponding template, something like this:
<TabControl.ContentTemplate>
<DataTemplate>
<ContentControl Content="{TemplateBinding Content}">
<ContentControl.Resources>
<DataTemplate DataType="{x:Type local:MySmellObject}">
<TextBox Text="{Binding Smell}" ></TextBox>
</DataTemplate>
<DataTemplate DataType="{x:Type local:MyTasteObject}">
<TextBox Text="{Binding Taste}" ></TextBox>
</DataTemplate>
</ContentControl.Resources>
</ContentControl>
</DataTemplate>
</TabControl.ContentTemplate>
Upvotes: 2