Custom Wpf Lookless contol...Dynamically Decide Control type
How to decide the type of a custom lookless control on run time.I have to decide the controls type(ie,whether textbox or combo) on runtime(actually when some Dependency property is bound).How can i do it? Can i define where 开发者_运维百科to inherit from on run time..?
You create a control that inherit from FramewrokElement (or Decorator, if you want a quick implementation and don't care about using a type for something it's not supposed to do) and create the required control as a child of your control when the dependency property is set.
You can use a Trigger that sets the ControlTemplate property of your control.
<Style TargetType={x:Type local:MyControl}>
<Style.Triggers>
<Trigger Property="MyProperty" Value="MyValue1">
<Setter Property="ControlTemplate">
<Setter.Value>
<ControlTemplate TargetType={x:Type local:MyControl}>
<!-- first template -->
</ControlTemplate
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="MyProperty" Value="MyValue2">
<Setter Property="ControlTemplate">
<Setter.Value>
<ControlTemplate TargetType={x:Type local:MyControl}>
<!-- second template -->
</ControlTemplate
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers
精彩评论