将DataTemplate绑定到CustomControl问题
我得到的是一个自定义控件,它在 xaml 中实现数据模板:
<DataTemplate x:Key="Templat">
<StackPanel>
<TextBlock Text="Sample Text" />
<TextBlock Text="{Binding Surname}" />
</StackPanel>
</DataTemplate>
并且自定义控件:
<local:MyControl x:Name="MyControl1"
ItemTemplate="{StaticResource Templat}" Margin="0,0,-24,8"/>
generic.xaml (在我的自定义控件库中)具有:
<ControlTemplate TargetType="local:MyControl">
<Canvas Name="LayoutRoot" Height="{TemplateBinding Height}"
Width="{TemplateBinding Width}"
Background="{TemplateBinding Background}"
CacheMode="BitmapCache">
<Canvas Name="ItemsHost" Margin="10,185,0,0" Height="615"
Width="{TemplateBinding Width}" CacheMode="BitmapCache">
<local:CustomItem x:Name="Item1"
ContentTemplate="{TemplateBinding ItemTemplate}" />
<local:CustomItem x:Name="Item2"
ContentTemplate="{TemplateBinding ItemTemplate}" />
</Canvas>
</Canvas>
</ControlTemplate>
我做错了什么?
我创建了一个自定义项目控件,其中有一些自定义内容控件。我希望它们都具有相同的内容模板,因此我将它们的内容模板绑定到父控件中定义的 itemtemplate。
我的问题是控件将显示带有“示例文本”文本的文本块,但不显示带有绑定值的文本块。我试图在代码隐藏中指定 DataContext (例如 DataContext = new Person() { Surname="Johnson" }
或通过 xaml。它们都不起作用。DataContext
(Person 类)看起来已正确传递,但传递的 DataTemplate 缺少 '{Binding Surname}'
表达式。您有什么想法可能是错误的吗?
What I've got is a custom control, which implements datatemplate in xaml:
<DataTemplate x:Key="Templat">
<StackPanel>
<TextBlock Text="Sample Text" />
<TextBlock Text="{Binding Surname}" />
</StackPanel>
</DataTemplate>
and custom control:
<local:MyControl x:Name="MyControl1"
ItemTemplate="{StaticResource Templat}" Margin="0,0,-24,8"/>
generic.xaml (in my custom control library) has:
<ControlTemplate TargetType="local:MyControl">
<Canvas Name="LayoutRoot" Height="{TemplateBinding Height}"
Width="{TemplateBinding Width}"
Background="{TemplateBinding Background}"
CacheMode="BitmapCache">
<Canvas Name="ItemsHost" Margin="10,185,0,0" Height="615"
Width="{TemplateBinding Width}" CacheMode="BitmapCache">
<local:CustomItem x:Name="Item1"
ContentTemplate="{TemplateBinding ItemTemplate}" />
<local:CustomItem x:Name="Item2"
ContentTemplate="{TemplateBinding ItemTemplate}" />
</Canvas>
</Canvas>
</ControlTemplate>
What am I doing wrong?
I created a custom item control, which has a few custom content controls inside. I want them all to have the same content template, so I bound their content template to defined itemtemplate in parent control.
My problem is that controls will show textblock with "Sample Text" text, but not the one with the binded value. I was trying to specify DataContext in codebehind (like DataContext = new Person() { Surname="Johnson" }
or via xaml. None of them worked.
The DataContext (Person class) looks like was passed correct, but passed DataTemplate misses the '{Binding Surname}'
expression. Have you got any ideas what can be wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
DataTemplate 中控件的 DataContext 不是从其父级继承的。因此,在您的情况下,
Templat
中的所有内容都将具有与 CustomItem 和 MyControl 不同的 DataContext。DataTemplate 的 DataContext 来自关联的 ContentControl 或 ContentPresenter 的 Content 属性。因此,在您的情况下,如果您这样做:
那么您可以设置 MyControl 的 DataContext,并且它将传递到您的 DataTemplate。
从表面上看,您的 MyControl 应该是一个 ItemsControl (如此处所述)。
The DataContext of the controls in the DataTemplate is not inherited from it's parent. So in your case, everything in the
Templat
will have a different DataContext than CustomItem and MyControl.The DataContext for the DataTemplate comes from the Content property of the associated ContentControl or ContentPresenter. So in your case, if you did this:
Then you could set the DataContext of MyControl, and it would get passed down to your DataTemplate.
From the looks of it though, your MyControl should be an ItemsControl though (as explained here).
当我编写使用数据模板的自定义控件时,我必须在自定义控件中编写代码以将模板绑定到模型。如果第一个答案不适合您,请检查此资源:
http ://chris.59north.com/post/Using-DataTemplates-in-custom-controls.aspx
When I've written custom controls that use data templates, I've had to write code in my custom controls to bind the template to the model. If the first answer does not work for you, check this resource:
http://chris.59north.com/post/Using-DataTemplates-in-custom-controls.aspx