Dro*_*ror 2 wpf binding datatemplate
我正在创建一个自定义控件,我遇到了将DataTemplate中的UI元素绑定到自定义控件的依赖项属性的问题.
我的控件中有一个内容控件,应该根据某个属性更改其内容和内容模板,所以我这样绑定它 -
<ContentControl Content="{TemplateBinding ControlMode}" ContentTemplateSelector="{StaticResource TemplateSelector}"/>
Run Code Online (Sandbox Code Playgroud)
内容模板选择器以这种方式定义 -
<ns:TemplateSelector x:Key="TemplateSelector">
<ns:TemplateSelector.Template1>
<DataTemplate>
<TreeView ItemsSource="{TemplateBinding TreeSource}"/>
</DataTemplate>
</ns:TemplateSelector.Template1>
<ns:TemplateSelector.Template2>
<DataTemplate>
<ListView ItemsSource="{TemplateBinding ListSource}"/>
</DataTemplate>
</ns:TemplateSelector.Template2>
</ns:TemplateSelector>
Run Code Online (Sandbox Code Playgroud)
问题是TreeView和ListView无法使用TemplateBinding绑定到他们的itemssource,因为例如这个错误 -
"在ContentPresenter类型上找不到TreeSourceProperty"
我一直在四处寻找答案,我发现这个简单的答案表明这是不可能的.
如何在自定义控件(Silverlight)中的数据模板中使用模板绑定
所以,如果这真的不可能,我怎样才能将模板中的元素绑定到CustomControl的DependencyProperties?
谢谢!
在WPF中,您可以使用绑定RelativeSource"模板化"控件.
例如
{Binding TreeSource,
RelativeSource={RelativeSource AncestorType=MyCustomControl}}
Run Code Online (Sandbox Code Playgroud)
编辑:如果你在树中休息,你可以通过传递控制来解决这个问题,例如
<ControlThatOwnsPopup
Tag="{Binding RelativeSource={RelativeSource AncestorType=MyCustomControl}}">
<Popup>...
Run Code Online (Sandbox Code Playgroud)
<TreeView ItemsSource="{Binding PlacementTarget.Tag.TreeSource,
RelativeSource={RelativeSource AncestorType=Popup}}"/>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3598 次 |
| 最近记录: |