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

2

1

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>
Allotropy answered 17/12, 2010 at 18:46 Comment(1)
This does not look right. You don't add a DataTemplate as a Content to a ContentControl. I don't think you can switch templates (easily) after setting one up? Instead, you can switch visibility of content inside one template to show/hide parts of it depending on data in DataContext.Universalist
A
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>
Alfano answered 17/12, 2010 at 20:5 Comment(14)
yeah Meleak the same link I found too but I got totally confused about TWO ContentControls used there. Just did not understand it why... I will test it tomorrow and let you guys know!Allotropy
@List: I agree. Check my updated answer to do it from the ContentControl insteadAlfano
@Lisa: I agree. Also I made an answer in the question I linked since the accepted answer there is a little missleading on how this should be doneAlfano
@Meleak yeah the guy in the other thread also used Styles. I tried your code and put it into my test project, but the DataTemplate is never switching? => sendspace.com/file/a427u1 Check it out for yourself, maybe I have overlooked something...Allotropy
@Lisa: The ContentControl doesn't have a DataContext so it has nothing to Bind against. Check the ctor. for MainWindow. It only has 'this.dataGrid1.DataContext = new PersonListViewModel();' so the DataContext won't be inherited or set for the ContentControlAlfano
@Lisa: That can easily happen :) You got the ContentTemplate to switch when you re-added it?Alfano
Ok everything works fine now. I switch the ContentTemplate now via IsNewPerson = true or false from my ViewModel where the logic is controlled ;-) thanks Meleak, seems I was on the wrong track this time(already used DataTemplateSelector in another context where it worked nicely) using DataTemplateSelector to switch the templates.Allotropy
Meleak are you still alive? one little thing (I expected it to really work but it does not after a test...) my UserControl seem to have no DataContext set. My "MainViewModel" has a property SelectedPersonViewModel (for the selected Person in the PersonListView.xaml) and CurrentPersonViewModel(for the new PersonView.xaml). I get no binding errors. I already tried stuff like that: <Style TargetType="ContentControl"><Setter Property="ContentTemplate" Value="{DynamicResource SelectedPersonId}" /><Setter Property="DataContext" Value="{Binding SelectedPersonViewModel}" /> Do you know the bug?Allotropy
The last Setter is what I tried. Set the appropriate DataContext when the View is switched. But the DataContext seems dead...Allotropy
@Lisa: Is the bug in the version which you uploaded yesterday?Alfano
sorry should have upload before: sendspace.com/file/kv9ant its V2. Just check it out everything seems dead... the manual setting of Property="DataContext" etc... is actually not needed but well I just tried to have at least a minimum chance hihiAllotropy
@Lisa: For some reason, the DataContext isn't inherited from the ContentControl to the View in the DataTemplate. This doesn't work even by setting the ContentTemplate directly and not using the Style. I'll update my answer with a workaround I got workingAlfano
@Meleak YES works! you are a rocket! ;P I have also snooped the views and saw the DataContext down the ContentControl is not inherited what is actually not possible or a BUG ? I will do a new question for this topic because it really interests me and maybe you too? Thanks again Meleak for helping out with your wpf knowledge.Allotropy
There you go Meleak => #4480888Allotropy
V
0

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/

Vada answered 20/12, 2010 at 8:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.