4

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

<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;

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

请帮忙..

4

2 回答 2

10

此方法将帮助您:

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");
    }
于 2013-11-11T13:39:09.497 回答
1

基本上,您需要提供一个(如错误所示)已Template应用的元素。你的ccBloodGroup控制在里面DataTemplate,所以很清楚,没有Template应用到它。

例如,可能Template应用了 this 的元素将是集合中使用 this来定义它们在 UI 中的外观ContentPresenter的项目的 s 。YourChoicesDataTemplate

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

// 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);

FindVisualChild方法显示在链接页面上。

于 2013-11-11T11:32:20.177 回答