Reputation: 708
Is this
WPF Control's Nested property's data binding
ever resolved in some way?
...
How can I:
I have this code:
<cars:2016Model
Engine.Power = "MOREMOREWARPSPEED"
>
public class 2016Model : Control
{
public 2016Model()
{
if (Date == 2016)
Engine = new 2016 Engine
}
public Engine { get; set; }
public class Engine
{
public virtual Double Power
get
{
return 0;
}
}
public class 2016Engine : Engine
{
public override Power
{
return 1000KWH;
}
}
public class 2015Engine : Engine
{
public override Power
{
return 350HP;
}
}
I Want to swap out the mid-level class.
I want to swap out the templates and have different views of the same current incarnation of Model and Engine.
I want to template this, but I cannot see how to set properties of a swappable class from the Xaml.
Upvotes: 0
Views: 391
Reputation: 5518
I'm not completely sure I understand what you want to do, but I have a suspicion.
First, your code doesn't compile for a variety of reasons -- among other things, you can't start an identifier with a digit. But that's a quibble -- assume these model objects:
public class Car2016Model : Control
{
public Engine Engine { get; set; }
}
public class Engine
{
public virtual double Power => 0;
}
public class Engine2016 : Engine
{
public override double Power => 1000;
}
public class Engine2015 : Engine
{
public override double Power => 350;
}
You can then declare cars in XAML with different engines as follows:
<cars:Car2016Model>
<cars:Car2016Model.Engine>
<cars:Engine2015 />
</cars:Car2016Model.Engine>
</cars:Car2016Model>
<cars:Car2016Model>
<cars:Car2016Model.Engine>
<cars:Engine2016 />
</cars:Car2016Model.Engine>
</cars:Car2016Model>
Of course, if you also want to display something useful, you need to provide a control template for the Cars2016Model
control:
<Grid.Resources>
<ResourceDictionary>
<ControlTemplate TargetType="cars:Car2016Model" x:Key="Template">
<Grid Background="Cyan">
<TextBox Text="{Binding Engine.Power, Mode=OneTime}" />
</Grid>
</ControlTemplate>
</ResourceDictionary>
</Grid.Resources>
Reference the template from the control:
<cars:Car2016Model Template="{StaticResource Template}">
<cars:Car2016Model.Engine>
<cars:Engine2015 />
</cars:Car2016Model.Engine>
</cars:Car2016Model>
I hope this is what you were asking.
Upvotes: 1