简体   繁体   English

自定义Wpf Lookless控制…动态确定控件类型

[英]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? 如何在运行时确定自定义无外观控件的类型。我必须在运行时(实际上是在绑定某些Dependency属性时)确定控件的类型(即文本框还是组合框)。我该怎么办? 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. 您可以使用触发器来设置控件的ControlTemplate属性。

<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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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