如何将TabItem内容设置为CommandTarget
我有一个绑定到集合的 TabControl 和 TabItems 的 DataTemplate。它工作得很好,但现在我想将 Button 的 CommandTarget 绑定到当前选定的 TabItem 上的 textEditor。我怎样才能做到这一点?
<TabControl Name="tabs" Grid.Row="1" Margin="0,5,0,0"
ItemsSource="{Binding Tabs, Mode=OneTime}">
<TabControl.ContentTemplate>
<DataTemplate DataType="vm:TabViewModel">
<avalon:TextEditor Name="textEditor"
Document="{Binding Document, Mode=OneWay}" Margin="3"
HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Auto"/>
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
I have a TabControl bound to a collection and a DataTemplate for the TabItems. It works pretty well, but now I want to bind Button's CommandTarget to textEditor on currently selected TabItem. How can I achive that?
<TabControl Name="tabs" Grid.Row="1" Margin="0,5,0,0"
ItemsSource="{Binding Tabs, Mode=OneTime}">
<TabControl.ContentTemplate>
<DataTemplate DataType="vm:TabViewModel">
<avalon:TextEditor Name="textEditor"
Document="{Binding Document, Mode=OneWay}" Margin="3"
HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Auto"/>
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我没有找到解决此问题的优雅方法,但解决方法很少。首先要从 DataTemplate 中查找控件,您必须使用 VisualTreeHelper 来查找 TabControl 的子控件。然后您可以将此机制打包到自定义 IValueConverter、行为或视图模型并作为 CommandTarget 传递
I didn't find an elegant way to resolve this issue, but there are few workarounds. First to find controls from DataTemplate you must use VisualTreeHelper to find child of TabControl. Then you can pack this mechanism to custom IValueConverter, behavior or viewmodel and pass as CommandTarget