简体   繁体   中英

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

您创建一个从FramewrokElement(或Decorator,如果您希望快速实现并且不关心将其用于不应该做的事情的类型)继承的控件,并在依赖属性时将所需的控件作为控件的子级创建被设置。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM