views:

107

answers:

2

Hi, 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..?

A: 

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
Jalfp
+1  A: 

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.

Nir