开发者

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

开发者 https://www.devze.com 2023-01-30 23:31 出处:网络
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

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" Prope开发者_运维知识库rty="ContentTemplate" Value="{DynamicResource SelectedPersonId}" />
                    </DataTrigger>
                </DataTemplate.Triggers>
            </DataTemplate>
        </ContentControl>


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>


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/

0

精彩评论

暂无评论...
验证码 换一张
取 消