简体   繁体   中英

WPF Content View not displaying DataTemplate

I have a ContentControl which will not display any XAML from its DataTemplate, and I feel certain that the problem I'm facing will be obvious for those with better WPF codemancy than myself. I have substituted "Object" for my object name where appropriate for confidentiality reasons.

In my MainWindow.xaml I have this:

<ContentControl x:Name="ObjectDetailView"
                Margin="20,20,20,20" Grid.Row="2" Grid.Column="1"
                DataContext="{Binding SelectedItem, ElementName=ObjectListView}"
                Template="{DynamicResource DetailControlTemplate}" 
                ContentTemplate="{DynamicResource DetailDataTemplate}"/>

I keep my templates in separate files to keep code readable. The template is in a DataResources.xaml file that is being successfully used for the ListView. The code for the content/template in question is:

<ControlTemplate x:Key="DetailControlTemplate">
    <Border Style="{StaticResource ObjectDetailBorderStyle}">
        <ContentPresenter/>
    </Border>
</ControlTemplate>

<DataTemplate x:Key="DetailDataTemplate" DataType="{x:Type model:Object}">
  <!-- Valid XAML -->
</DataTemplate>

In my Designer (both in VS and Blend) The border/background gradient displays, but nothing further. Same for the running program.

If I move the <!-- Valid XAML --> into the Control Template, it displays fine, but I don't believe that's kosher, and I also don't believe that the data-binding will work that way. Please correct me if I'm wrong.

ObjectListView is a ListView populated dynamically from my VM, and I'm using MVVM. That all works just fine. I'd prefer this ContentControl only appears once there is a valid selected object in the list view, but that's UX sugar at this point, thus my only concern is to get this content control displaying my model's data.

I'm also fairly new to StackOverflow, so if I missed anything or made an error in posting this question, please let me know. I've not had luck with searching for this issue over the last few hours, as I don't want to waste your time.

Two things. You did not set the actual Content of the ContentControl, but only its DataContext. You should instead write this:

<ContentControl ...
    Content="{Binding SelectedItem, ElementName=ObjectListView}"
    Template="{DynamicResource DetailControlTemplate}" 
    ContentTemplate="{DynamicResource DetailDataTemplate}"/>

And your ControlTemplate is missing a TargetType :

<ControlTemplate x:Key="DetailControlTemplate" TargetType="ContentControl">
    <Border Style="{StaticResource ObjectDetailBorderStyle}">
        <ContentPresenter/>
    </Border>
</ControlTemplate>

Without the TargetType, the ContentPresenter's properties aren't set by default, and you would have to set them explicitly like

<ControlTemplate x:Key="DetailControlTemplate">
    <Border Style="{StaticResource ObjectDetailBorderStyle}">
        <ContentPresenter
            Content="{Binding Content,
                      RelativeSource={RelativeSource TemplatedParent}}"
            ContentTemplate="{Binding ContentTemplate,
                              RelativeSource={RelativeSource TemplatedParent}}"/>
    </Border>
</ControlTemplate>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM