WPF: Can not find the Trigger target 'cc'. The target must appear before any Setters, Triggers

元气小坏坏 提交于 2019-12-11 06:37:30

问题


what is wrong about the following code?

I get this error during compilation:

The property 'TargetName' does not represent a valid target for the 'Setter' because an element named 'cc' was not found. Make sure that the target is declared before any Setters, Triggers or Conditions that use it.

How do I have to refactor my code so I can compile it without error?

I just want to switch a datatemplate with DataTrigger bound to a value in my PersonViewModel!

 <ContentControl x:Name="cc" Grid.Column="1">
            <DataTemplate>
                <DataTemplate.Triggers>
                    <DataTrigger Binding="{Binding Path=CurrentPersonViewModel.IsNew}" Value="True">
                        <Setter TargetName="cc" Property="ContentTemplate" Value="{DynamicResource NewPersonId}" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Path=CurrentPersonViewModel.IsNew}" Value="False">
                        <Setter TargetName="cc" Property="ContentTemplate" Value="{DynamicResource SelectedPersonId}" />
                    </DataTrigger>
                </DataTemplate.Triggers>
            </DataTemplate>
        </ContentControl>

回答1:


Update

You can use a Style for the ContentControl and change the ContentTemplate from there

<ContentControl Name="cc" Grid.Column="1">
    <ContentControl.Style>
        <Style TargetType="ContentControl">
            <Setter Property="ContentTemplate" Value="{DynamicResource SelectedPersonId}"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=CurrentPersonViewModel.IsNew}" Value="True">
                    <Setter Property="ContentTemplate" Value="{DynamicResource NewPersonId}" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ContentControl.Style>
</ContentControl>

UPDATE
I don't understand why the View's in the DataTemplate doesn't inherit the DataContext. Got it working by using this but I can't see why this is necessary

<DataTemplate x:Key="NewPersonId">
    <local:NewPersonView DataContext="{Binding RelativeSource={RelativeSource AncestorType={x:Type ContentControl}}, Path=DataContext.CurrentPersonViewModel}" />
</DataTemplate>

<DataTemplate x:Key="SelectedPersonId">
    <local:SelectedPersonView DataContext="{Binding RelativeSource={RelativeSource AncestorType={x:Type ContentControl}}, Path=DataContext.SelectedPersonViewModel}"/>
</DataTemplate>



回答2:


You do not need the whole DataTrigger stuff.

Just read this to make your DataTemplateSelector work properly:

http://joshsmithonwpf.wordpress.com/2007/03/18/updating-the-ui-when-binding-directly-to-business-objects-that-are-modified/



来源:https://stackoverflow.com/questions/4473796/wpf-can-not-find-the-trigger-target-cc-the-target-must-appear-before-any-set

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!