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>
Elisabeth
  • 20,496
  • 52
  • 200
  • 321
  • 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`. – decyclone Dec 17 '10 at 19:00

2 Answers2

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>
Fredrik Hedblad
  • 83,499
  • 23
  • 264
  • 266
  • 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! – Elisabeth Dec 17 '10 at 20:29
  • @List: I agree. Check my updated answer to do it from the ContentControl instead – Fredrik Hedblad Dec 17 '10 at 20:56
  • @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 done – Fredrik Hedblad Dec 17 '10 at 21:47
  • @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? => http://www.sendspace.com/file/a427u1 Check it out for yourself, maybe I have overlooked something... – Elisabeth Dec 17 '10 at 21:53
  • @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 ContentControl – Fredrik Hedblad Dec 17 '10 at 23:41
  • @Lisa: That can easily happen :) You got the ContentTemplate to switch when you re-added it? – Fredrik Hedblad Dec 18 '10 at 08:55
  • 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. – Elisabeth Dec 18 '10 at 10:51
  • 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: – Elisabeth Dec 18 '10 at 16:20
  • The last Setter is what I tried. Set the appropriate DataContext when the View is switched. But the DataContext seems dead... – Elisabeth Dec 18 '10 at 16:21
  • @Lisa: Is the bug in the version which you uploaded yesterday? – Fredrik Hedblad Dec 18 '10 at 16:27
  • sorry should have upload before: http://www.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 hihi – Elisabeth Dec 18 '10 at 16:33
  • @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 working – Fredrik Hedblad Dec 18 '10 at 18:00
  • @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. – Elisabeth Dec 18 '10 at 20:58
  • There you go Meleak => http://stackoverflow.com/questions/4480388/frameworkelements-datacontext-property-does-not-inherit-down-the-element-tree – Elisabeth Dec 18 '10 at 21:56
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/

msfanboy
  • 5,273
  • 13
  • 69
  • 120