Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何基于依赖项属性交换控件?_C#_Wpf_Templates_Triggers_Custom Controls - Fatal编程技术网

C# 如何基于依赖项属性交换控件?

C# 如何基于依赖项属性交换控件?,c#,wpf,templates,triggers,custom-controls,C#,Wpf,Templates,Triggers,Custom Controls,我正在创建一个自定义控件 我希望此控件的模板根据名为CanExpand的依赖项属性的值为根控件使用不同的控件。CanExpand是在自定义控件类中定义的 如果CanExpand为true,我希望使用扩展器显示: <ControlTemplate ...> <Expander ...> <!--...--> <ContentPresenter/> </Expander> </ControlTemp

我正在创建一个自定义控件

我希望此控件的模板根据名为CanExpand的依赖项属性的值为根控件使用不同的控件。CanExpand是在自定义控件类中定义的

如果CanExpand为true,我希望使用扩展器显示:

<ControlTemplate ...>
   <Expander ...>
      <!--...-->
      <ContentPresenter/>
   </Expander>
</ControlTemplate>

如果CanExpand为false,我希望改为使用HeaderedContentControl显示:

<ControlTemplate ...>
   <HeaderedContentControl ...>
      <!--...-->
      <ContentPresenter/>
   </HeaderedContentControl>
</ControlTemplate>

我曾想过使用DataTemplateSelector,但这是一个ControlTemplate而不是DataTemplate,并且控件的模板没有selector属性

我无法使用触发器将不同的控件设置为可见/隐藏,因为子内容只能在一个控件下存在。另外,我认为不能使用触发器更改内容属性

有什么建议吗


谢谢。

在您的样式中,将ControlTemplate属性设置为默认状态,然后使用触发器将ControlTemplate属性设置为其他模板。例如:

<Style ...>
    <Setter Property="ControlTemplate">
        <ControlTemplate ...>    
        </ControlTemplate>
    </Setter>
    <Style.Triggers>
        <Trigger Property="YourProperty" Value="WhateverValue">
            <Setter Property="ControlTemplate">
                <ControlTemplate ...>
                </ControlTemplate>
            </Setter>
        </Trigger>
    </Style.Triggers>
</Style>


请记住,对于多个值,可以在同一属性上使用触发器,每个值获得完全不同的模板。

我以为只能在样式中使用EventTriggers?不,样式中允许使用属性触发器。当您直接在元素上放置触发器时,您只能使用EventTriggers。