简体   繁体   English

WPF CustomControl切换ContentControl的内容

[英]WPF CustomControl switch content of ContentControl

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfCustomControlLibrary1">

<ContentControl x:Key="BackSide" Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Back}" RenderTransformOrigin="0.5,0.5">
    <ContentControl.RenderTransform>
        <ScaleTransform ScaleX="-1" />
    </ContentControl.RenderTransform>
</ContentControl>

<Style TargetType="{x:Type local:CustomControl1}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:CustomControl1}">
                <ContentControl Grid.Row="1">
                    <ContentControl.RenderTransform>
                        <TransformGroup>
                            <ScaleTransform x:Name="tf" ScaleX="1" />
                        </TransformGroup>
                    </ContentControl.RenderTransform>
                    <ContentControl.Style>
                        <Style TargetType="ContentControl">
                            <Setter Property="Content" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Front}" />
                            <Style.Triggers>
                                <DataTrigger Value="True">
                                    <DataTrigger.Binding>
                                        <Binding ElementName="tf" Path="ScaleX">
                                            <Binding.Converter>
                                                <loc:LessThanXToTrueConverter X="0" />
                                            </Binding.Converter>
                                        </Binding>
                                    </DataTrigger.Binding>
                                    <DataTrigger.Setters>
                                        <Setter Property="Content" Value="{StaticResource BackSide}"/>
                                    </DataTrigger.Setters>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </ContentControl.Style>
                </ContentControl>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

This is some XAML code from a customcontrol. 这是来自customcontrol的一些XAML代码。 Where there are two dependency properties (Front and Back). 那里有两个依赖项属性(正面和背面)。

Via my DataTrigger I want to change the Content of the ContentControl from using "Front" to using "Back". 通过我的DataTrigger,我想将ContentControl的内容从“ Front”更改为“ Back”。 At first it shows the depencency property "Front" and then it should use the depencency property "Back" as the Content. 首先,它显示依赖属性“ Front”,然后应使用依赖属性“ Back”作为内容。 This is done via this code: 这是通过以下代码完成的:

<DataTrigger.Setters>
  <Setter Property="Content" Value="{StaticResource BackSide}"/>
</DataTrigger.Setters>

But this doesn't work... 但这行不通...

I can bind and display the content of the Front dependency property in my control via: 我可以通过以下方式绑定并显示控件中Front依赖项属性的内容:

<Setter Property="Content" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Front}" />

but I can't figure out how to bind the DataTrigger setter so that it uses the ContentControl's Content with the x:Key="BackSide" ContentControl. 但我不知道如何绑定DataTrigger设置程序,以便它使用ContentControl的Content和x:Key =“ BackSide” ContentControl。

Thanks in advance. 提前致谢。

You said that this XAML works: 您说此XAML有效:

<Setter Property="Content" Value="{Binding RelativeSource={RelativeSource 
    TemplatedParent}, Path=Front}" />

So why don't you just try putting this XAML into your DataTrigger ?: 那么,为什么不尝试将XAML放入DataTrigger呢?:

<Setter Property="Content" Value="{Binding RelativeSource={RelativeSource 
    TemplatedParent}, Path=Back}" />

UPDATE >>> 更新>>>

I'm not really sure what you're trying to do with your UIElement DependencyProperty s, but I am guessing that you are taking the wrong approach. 我不太确定您要如何使用UIElement DependencyProperty ,但是我猜测您使用的方法错误。 Typically in WPF, our properties are data types, not UI types and then we generate UI types using DataTemplate s... perhaps you should re-think your approach. 通常,在WPF中,我们的属性是数据类型,而不是UI类型,然后我们使用DataTemplate生成UI类型。也许您应该重新考虑您的方法。

Please see the Data Templating Overview page on MSDN for further help. 请参阅MSDN上的“ 数据模板概述”页面以获取更多帮助。

You might also find my answer to the WPF MVVM navigate views question helpful to read. 您可能还会发现我对WPF MVVM导航视图问题的回答有助于阅读。

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

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