[英]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. 我有一个ContentControl,它不会从其DataTemplate中显示任何XAML,而且我确信,对于WPF编码比我自己更好的人,我面临的问题将很明显。 I have substituted "Object" for my object name where appropriate for confidentiality reasons.
出于机密原因,我在适当的地方用“ Object”代替了我的对象名称。
In my MainWindow.xaml I have this: 在我的MainWindow.xaml中,我有以下内容:
<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.
该模板位于DataResources.xaml文件中,该文件已成功用于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. 在我的设计器中(同时在VS和Blend中)显示边框/背景渐变,但没有其他内容。 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. 如果将
<!-- Valid XAML -->
移到控制模板中,它会显示正常,但是我不认为这是洁食,而且我也不相信数据绑定会那样工作。 Please correct me if I'm wrong. 如果我错了,请纠正我。
ObjectListView is a ListView populated dynamically from my VM, and I'm using MVVM. ObjectListView是从我的VM动态填充的ListView,并且我正在使用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.
我希望此ContentControl仅在列表视图中有一个有效的选定对象时才出现,但这是UX糖,因此,我唯一关心的是使此内容控件显示我的模型数据。
I'm also fairly new to StackOverflow, so if I missed anything or made an error in posting this question, please let me know. 我也是StackOverflow的新手,所以如果我错过了任何内容或在发布此问题时出错了,请告诉我。 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. 您没有设置ContentControl的实际
Content
,而是仅设置了它的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缺少
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 如果没有TargetType,则默认情况下不会设置ContentPresenter的属性,您必须像
<ControlTemplate x:Key="DetailControlTemplate">
<Border Style="{StaticResource ObjectDetailBorderStyle}">
<ContentPresenter
Content="{Binding Content,
RelativeSource={RelativeSource TemplatedParent}}"
ContentTemplate="{Binding ContentTemplate,
RelativeSource={RelativeSource TemplatedParent}}"/>
</Border>
</ControlTemplate>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.