C# WPF CustomControl切换ContentControl的内容

C# WPF CustomControl切换ContentControl的内容,c#,wpf,xaml,data-binding,custom-controls,C#,Wpf,Xaml,Data Binding,Custom Controls,这是来自customcontrol的一些XAML代码。其中有两个依赖项属性(前面和后面) 通过我的DataTrigger,我想将ContentControl的内容从使用“Front”更改为使用“Back”。 它首先显示依赖性属性“Front”,然后应该使用依赖性属性“Back”作为内容。 这是通过以下代码完成的: <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xml

这是来自customcontrol的一些XAML代码。其中有两个依赖项属性(前面和后面)

通过我的DataTrigger,我想将ContentControl的内容从使用“Front”更改为使用“Back”。 它首先显示依赖性属性“Front”,然后应该使用依赖性属性“Back”作为内容。 这是通过以下代码完成的:

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

但这不起作用

我可以通过以下方式绑定和显示控件中Front dependency属性的内容:

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

但是我不知道如何绑定DataTrigger setter,以便它使用ContentControl的内容和x:Key=“BackSide”ContentControl。


提前感谢。

您说过这个XAML可以工作:

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

更新>>>

我真的不确定你想用
ui元素依赖属性做什么,但我猜你采取了错误的方法。通常在WPF中,我们的属性是数据类型,而不是UI类型,然后我们使用
DataTemplate
s。。。也许你应该重新考虑你的方法

有关更多帮助,请参阅MSDN上的页面


您可能会发现我对这个问题的回答对阅读很有帮助。

您的输出窗口中是否有任何绑定错误?您是否也确保datatrigger的值确实为true?如果我将RelativeSource TemplatedParent的Setter设置为相同的值,则与Front属性不同(正如Sheridan建议的那样)我看到了内容,但没有应用我的RenderTransform ScaleTransform。我已经这样做了,但由于某些原因,在本例中忽略了RenderTransform/ScaleTransform。除非需要额外的东西,否则,
RenderTransform
会像这样被忽略,因为我们在
Resources
中只引用属性,而不是
ContentControl
。任何代码示例都可以解释这一点,因为我现在很困惑以上是一个代码示例。它引用了你的
后台依赖属性
,而不是你的
后台内容控件
。好吧,我搞糊涂了。但如果我引用背面,我看不到任何内容。为什么我不能使用BackSide及其定义的RenderTransform/ScaleTransform?ContentControl本身也是一个UIElement,就像dep.属性一样。
<Setter Property="Content" Value="{Binding RelativeSource={RelativeSource 
    TemplatedParent}, Path=Front}" />
<Setter Property="Content" Value="{Binding RelativeSource={RelativeSource 
    TemplatedParent}, Path=Back}" />