从代码后面分配 DataTemplate 时 ElementName 不起作用?
我正在尝试使用 DataTemplate 中的 ElementName 访问 Control,该 DataTemplate 用于与定义(在 xaml 中)不同的 UserControl(资源)中。
想象一下这种情况:
MyUserControl.xaml 在资源中具有以下 DataTemplate:
<UserControl.Resources>
<DataTemplate x:Key="SomeTemplate">
<TextBlock Text="{Binding Text, ElementName=TextElement}"/>
</DataTemplate>
</UserControl.Resources>
<Grid x:Name="LayoutRoot">
<TextBlock x:Name="TextElement" Text="IT WORKS! (not...)"/>
</Grid>
</UserControl>
MyUserControlWrapper.xaml
<ContentPresenter x:Name="ContentPresenter" Content="{Binding SomeContent}"/>
并在 MyUserControlWrapper.xaml 的代码后面我从 MyUserControl.xaml 设置 ContentPresenter 的 ContentTemplate:
类似于:
ContentPresenter.ContentTemplate = (DataTemplate)childView.Resources["SomeTemplate"];
是否可以使用在 UserControl 外部定义的资源中的 ElementName ?
那么DataTemplate如何在同一个UserControl中搜索ElementName呢?也许可以为 DataTemplate 本身设置类似 DataContext 的内容,以便 ElementName 正常工作,而不会干扰发送到 Template 内使用的控件的 DataContext?
I am trying to access Control using ElementName from DataTemplate that is used in different UserControl (Resources) than defined (in xaml).
Imagine this situation:
MyUserControl.xaml with following DataTemplate in resources:
<UserControl.Resources>
<DataTemplate x:Key="SomeTemplate">
<TextBlock Text="{Binding Text, ElementName=TextElement}"/>
</DataTemplate>
</UserControl.Resources>
<Grid x:Name="LayoutRoot">
<TextBlock x:Name="TextElement" Text="IT WORKS! (not...)"/>
</Grid>
</UserControl>
MyUserControlWrapper.xaml
<ContentPresenter x:Name="ContentPresenter" Content="{Binding SomeContent}"/>
and in code behind of MyUserControlWrapper.xaml i set ContentTemplate of ContentPresenter from MyUserControl.xaml:
something like:
ContentPresenter.ContentTemplate = (DataTemplate)childView.Resources["SomeTemplate"];
Is it possible to use ElementName from resources that are defined outside UserControl?
How DataTemplate searches for ElementName in same UserControl then? Maybe its possible to set something like DataContext for DataTemplate itself for ElementName to work, without messing with DataContext that is sent to controls used inside Template?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要查看与 名称范围。
简而言之,名称的作用域是加载 Xaml 资源的位置。例如,每个 UserControl 将各自加载自己的 Xaml,因此具有自己的名称范围。在您的情况下,您要求 MyUserControlWrapper 查找其
LoadComponent
未见过的名称。You need to review the concepts related to Namescopes.
Briefly names are scoped at the point where a Xaml resources are loaded. For example each UserControl will each load their own Xaml and therefore have their own namescope. In your case you asking
MyUserControlWrapper
to find a name that itsLoadComponent
has not seen.也许您可以使用RelativeSource 和FindAncestor 沿着VisualTree 向上走?
这里有一个关于不同绑定变体的很好的介绍:
http://www.wpfwiki.com/Default。 aspx?Page=WPF%20Q5.3&AspxAutoDetectCookieSupport=1
Maybe you can just walk up the VisualTree using RelativeSource and FindAncestor?
There is a nice presentation of different binding variants here:
http://www.wpfwiki.com/Default.aspx?Page=WPF%20Q5.3&AspxAutoDetectCookieSupport=1