WPF如何从的DataTemplate访问控制 [英] WPF How to access control from DataTemplate
问题描述
我有一个DataTemplate其中包含一个网格,在网格内我有一个组合框。
I have a datatemplate which contains a grid and inside the grid I have a combobox.
<DataTemplate x:Key="ShowAsExpanded">
<Grid>
<ComboBox Name ="myCombo" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Top" Margin="5"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding}"
ItemTemplate="{StaticResource MyItems}">
<ComboBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel />
</ItemsPanelTemplate>
</ComboBox.ItemsPanel>
</ComboBox>
</Grid>
</DataTemplate>
我然后我有指模板,通过造型的网格。
I then I have a grid that refers to that template through styling.
<Grid>
<ContentPresenter Name="_contentPresenter" Style="{DynamicResource StyleWithCollapse}" Content="{Binding}" />
</Grid>
我如何能够通过code访问behing的myCombo基本上设置其DataContext的?
How can I access through code behing the myCombo to basically set its DataContext?
推荐答案
首先,我甚至不能找到资源(ShowAsExpanded)和里面的内容presenter的使用之间的关系。但就目前而言,我们假设DynamicResource应指向ShowAsExpanded。
First of all, I can't even find the relation between the Resource (ShowAsExpanded) and the usage inside the ContentPresenter. But for the moment, let's assume that the DynamicResource should point to ShowAsExpanded.
您不能并通过code不应访问的组合框。你应该在DataContext绑定到使用该样式的网格。如果你不想这样做,你将不得不寻找在运行时的内容和搜索子组合框。
You can't and shouldn't access the combobox via code. You should bind the datacontext to the grid that uses the style. If you don't want to do that, you will have to find the content at runtime and search for the child combobox.
这篇关于WPF如何从的DataTemplate访问控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!