In my opinion tagaPdyk's answer is correct, but I think a sample can explain better how to do.
There is no need of reflection. The idea is to combine implicit DataTemplates with a ContentPresenter.
Let's suppose we have to types of data: Data1 and Data2. Here their code:
public class Data1
{
public string Name { get; set; }
public string Description { get; set; }
}
public class Data2
{
public string Alias { get; set; }
public Color Color { get; set; }
}
Now I create a simple ViewModel:
public class ViewModel : PropertyChangedBase
{
private Data1 data1 = new Data1();
private Data2 data2 = new Data2();
private object current;
private RelayCommand switchCommand;
public ViewModel1()
{
switchCommand = new RelayCommand(() => Switch());
Current = data1;
}
public ICommand SwitchCommand
{
get
{
return switchCommand;
}
}
public IEnumerable<Color> Colors
{
get
{
List<Color> colors = new List<Color>();
colors.Add(System.Windows.Media.Colors.Red);
colors.Add(System.Windows.Media.Colors.Yellow);
colors.Add(System.Windows.Media.Colors.Green);
return colors;
}
}
private void Switch()
{
if (Current is Data1)
{
Current = data2;
return;
}
Current = data1;
}
public object Current
{
get
{
return current;
}
set
{
if (current != value)
{
current = value;
NotifyOfPropertyChange("Current");
}
}
}
}
where PropertyChangedBase is a base implementation class of INotifyPropertyChanged.
Now the most important - for this question - part, i.e. the XAML
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<CollectionViewSource x:Key="colors" Source="{Binding Path=Colors, Mode=OneTime}" />
<DataTemplate DataType="{x:Type local:Data1}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Text="Name" VerticalAlignment="Center" />
<TextBox Text="{Binding Name}" Grid.Column="1" Margin="5" />
<TextBlock Text="Description" Grid.Row="1" VerticalAlignment="Center" />
<TextBox Text="{Binding Description}" Grid.Column="1" Grid.Row="1" Margin="5" />
</Grid>
</DataTemplate>
<DataTemplate DataType="{x:Type local:Data2}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Text="Alias" VerticalAlignment="Center" />
<TextBox Text="{Binding Alias}" Grid.Column="1" Margin="5" />
<TextBlock Text="Color" Grid.Row="1" VerticalAlignment="Center" />
<ComboBox Text="{Binding Color}" Grid.Column="1" Grid.Row="1" Margin="5"
ItemsSource="{Binding Source={StaticResource colors}}" />
</Grid>
</DataTemplate>
</Window.Resources>
<StackPanel>
<ContentPresenter Content="{Binding Path=Current}" />
<Button Content="Switch" Command="{Binding SwitchCommand}" Margin="30" />
</StackPanel>
</Window>
As you can see I define a DataTemplate for each object I want to handle in the ContentPresenter. I have to set the DataType property for each DataTemplate. In this way the proper template will be automatically used inside the ContentPresenter (depending on the type of the object binded to its DataContext).
You can use the "Switch" button to switch between a Data1 object and a Data2 one. Moreover if you look at the Output Window of your VS, you will see no messages about binding errors.
I hope my sample can help with your issue.
EDIT
I placed the emphasis of my answer on the fact that with DataTemplates you have binding errors no more. There are not so many differences between objects with common properties and objects without them.
Anyway, let's suppose that both Data1 and Data2 derive from BaseData class. Here its simple code:
public class BaseData
{
public bool IsValid { get; set; }
}
In this way IsValid is a common property for Data1 and Data2. Now you can choose between two possible solutions:
- You add
IsValid property to both the "implicit" DataTemplates.
- You create a "base" DataTemplate (for
BaseData objects) and the you reuse it in the "implicit" DataTemplates (pro: you have to write less XAML - cons: it can have an impact on the UI performance)
Regarding the second solution, your DataTemplates will become:
<Window.Resources>
<CollectionViewSource x:Key="colors" Source="{Binding Path=Colors, Mode=OneTime}" />
<DataTemplate x:Key="{x:Type local:BaseData}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Text="Is valid" VerticalAlignment="Center" />
<CheckBox IsChecked="{Binding IsValid}" Margin="5" Grid.Column="1" />
</Grid>
</DataTemplate>
<DataTemplate DataType="{x:Type local:Data1}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Text="Name" VerticalAlignment="Center" />
<TextBox Text="{Binding Name}" Grid.Column="1" Margin="5" />
<TextBlock Text="Description" Grid.Row="1" VerticalAlignment="Center" />
<TextBox Text="{Binding Description}" Grid.Column="1" Grid.Row="1" Margin="5" />
<ContentPresenter Grid.Row="2" Grid.ColumnSpan="2"
ContentTemplate="{StaticResource {x:Type local:BaseData}}" />
</Grid>
</DataTemplate>
<DataTemplate DataType="{x:Type local:Data2}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Text="Alias" VerticalAlignment="Center" />
<TextBox Text="{Binding Alias}" Grid.Column="1" Margin="5" />
<TextBlock Text="Color" Grid.Row="1" VerticalAlignment="Center" />
<ComboBox Text="{Binding Color}" Grid.Column="1" Grid.Row="1" Margin="5"
ItemsSource="{Binding Source={StaticResource colors}}" />
<ContentPresenter Grid.Row="2" Grid.ColumnSpan="2"
ContentTemplate="{StaticResource {x:Type local:BaseData}}" />
</Grid>
</DataTemplate>
</Window.Resources>
ConfigType1has only one propertyA,ConfigType2has only one propertyB, etc. (see comments in my code). Idea is to use same view where for each possible property there are controls with binding. And when configuration is changed no binding errors should occurs (and only those controls should be visible, which currentConfighas). - SinatrBindingitself? Instead of binding/unbinding use some of its mechanisms (if there is any) to disable it and to resolve binding errors myself. Is it possible? - Sinatr