如何在内容控件中获取元素

时间:2013-11-11 11:15:56

标签: c# wpf xaml

我需要在内容控件中找到元素:

<ContentControl Content="{Binding YourChoices}" Grid.ColumnSpan="3" x:Name="ccBloodGroup">
                <ContentControl.ContentTemplate>
                    <DataTemplate>
                        <Grid>
                            <ComboBox x:Name="cbBloodGroup" ItemsSource="{Binding}" HorizontalAlignment="Left" Margin="10,160,0,0" VerticalAlignment="Top" Width="331" Height="45">
                                <ComboBoxItem>A+</ComboBoxItem>
                                <ComboBoxItem>A-</ComboBoxItem>
                                <ComboBoxItem>B+</ComboBoxItem>
                                <ComboBoxItem>B-</ComboBoxItem>
                                <ComboBoxItem>O+</ComboBoxItem>
                                <ComboBoxItem>O-</ComboBoxItem>
                                <ComboBoxItem>AB+</ComboBoxItem>
                                <ComboBoxItem>AB-</ComboBoxItem>
                            </ComboBox>
                            <TextBlock x:Name="tb" Text=" Blood Type" IsHitTestVisible="False" Visibility="Hidden" HorizontalAlignment="Left" Margin="10,176,0,0" VerticalAlignment="Top"/>
                        </Grid>
                        <DataTemplate.Triggers>
                            <Trigger SourceName="cbBloodGroup" Property="SelectedItem" Value="{x:Null}">
                                <Setter TargetName="tb" Property="Visibility" Value="Visible"/>
                            </Trigger>
                        </DataTemplate.Triggers>
                    </DataTemplate>
                </ContentControl.ContentTemplate>
            </ContentControl>

我在互联网上找到了答案

 ComboBox cb = ccBloodGroup.ContentTemplate.FindName("cbBloodGroup", ccBloodGroup) as ComboBox;

但是这给了我一个运行时异常,说'此操作仅对已应用此模板的元素有效'。

请帮助..

2 个答案:

答案 0 :(得分:6)

此方法可以帮助您:

public T FindElementByName<T>(FrameworkElement element, string sChildName) where T : FrameworkElement
    {
            T childElement = null;
            var nChildCount = VisualTreeHelper.GetChildrenCount(element);
            for (int i = 0; i < nChildCount; i++)
            {
                FrameworkElement child = VisualTreeHelper.GetChild(element, i) as FrameworkElement;

                if (child == null)
                    continue;

                if (child is T && child.Name.Equals(sChildName))
                {
                    childElement = (T)child;
                    break;
                }

                childElement = FindElementByName<T>(child, sChildName);

                if (childElement != null)
                    break;
            } 
            return childElement;
    }

而且,我如何使用它,只需添加按钮,然后点击按钮

 private void Button_OnClick(object sender, RoutedEventArgs e)
    {
        var element = FindElementByName<ComboBox>(ccBloodGroup, "cbBloodGroup");
    }

答案 1 :(得分:1)

基本上,您需要提供一个(如错误所示)已应用Template的元素。您的ccBloodGroup控件位于DataTemplate内,因此很明显,没有应用此Template

例如,可能已应用此Template的元素将是ContentPresenter集合中使用此YourChoices到的DataTemplate项的// Getting the currently selected ListBoxItem // Note that the ListBox must have // IsSynchronizedWithCurrentItem set to True for this to work ListBoxItem myListBoxItem = (ListBoxItem)(myListBox.ItemContainerGenerator. ContainerFromItem(myListBox.Items.CurrentItem)); // Getting the ContentPresenter of myListBoxItem ContentPresenter myContentPresenter = FindVisualChild<ContentPresenter>(myListBoxItem); // Finding textBlock from the DataTemplate that is set on that ContentPresenter DataTemplate myDataTemplate = myContentPresenter.ContentTemplate; TextBlock myTextBlock = (TextBlock)myDataTemplate.FindName("textBlock", myContentPresenter); // Do something to the DataTemplate-generated TextBlock MessageBox.Show("The text of the TextBlock of the selected list item: " + myTextBlock.Text); 个在UI中定义它们的样子。

你可以像往常一样在MSDN上找到完整的详细信息,在FrameworkTemplate.FindName Method页面上有一个详细的例子,但它是这样的......来自链接页面上的例子:

FindVisualChild
  

{{1}}方法显示在链接页面上。