绑定到 DataTemplate 内的模板属性
我有一个自定义控件,如下所示:
<CustomControl>
<CustomControl.ContentTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Name}"/>
</StackPanel>
</DataTemplate>
</CustomControl.ContentTemplate>
</CustomControl>
在 CustomControl 的控件模板中,我尝试从 DataTemplate 内绑定到 CustomControl.ContentTemplate,但它不起作用:
<ListBox
ItemsSource="{Binding SearchResultsList}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<ContentControl
Content="{Binding}"
ContentTemplate="{TemplateBinding ContentTemplate}">
</ContentControl>
<ItemsControl
ItemsSource="{Binding HierarchyPath}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="->"/>
<TextBlock Text="{Binding Name}"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
注意:ContentTemplate="{TemplateBinding ContentTemplate}"< /strong>
我知道您不能在 Datatemplate 内使用 TemplateBinding,即使 DataTemplate 位于控件模板内。但有谁知道如何在不使用 TemplateBinding 的情况下实现我想要实现的目标?
I have a custom control as follows:
<CustomControl>
<CustomControl.ContentTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Name}"/>
</StackPanel>
</DataTemplate>
</CustomControl.ContentTemplate>
</CustomControl>
In the control template of the CustomControl, I try to bind to the CustomControl.ContentTemplate from within a DataTemplate, but it does not work:
<ListBox
ItemsSource="{Binding SearchResultsList}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<ContentControl
Content="{Binding}"
ContentTemplate="{TemplateBinding ContentTemplate}">
</ContentControl>
<ItemsControl
ItemsSource="{Binding HierarchyPath}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="->"/>
<TextBlock Text="{Binding Name}"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Note: ContentTemplate="{TemplateBinding ContentTemplate}"
I know that you cannot use TemplateBinding inside a Datatemplate, even though the DataTemplate is inside a control template. But does anyone know how to achieve what I want to achieve without using TemplateBinding?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最好的选择是使用 ListBox.ItemContainerStyle,并用它来制作 ControlTemplate。
像这样的东西:
Your best bet is to use the ListBox.ItemContainerStyle, and use it to make a ControlTemplate.
Something like this: