none
如何用C#(C++)访问HubSection中的元素? RRS feed

  • 问题

  • xaml(部分):

    <Hub>
        <HubSection x:Name="HSname" x:Uid="HSuid">
            <DataTemplate>
                <TextBlock Text="你好">
            </DataTemplate>
        </HubSection>
    </Hub>

    要求:将“你好”变为“再见”,使用C#(C++)代码

    (我试过给TextBlock命名,但是无法访问到)
    2014年8月25日 12:55

答案

  • 楼主 你好,

    建议你参考这篇文档,http://msdn.microsoft.com/zh-cn/library/bb613579(v=vs.110).aspx

    这里有一个WPF的例子,请参考:

    XAML:

    <Window.Resources>
            <XmlDataProvider x:Key="InventoryData" XPath="Inventory/Books">
                <x:XData>
                    <Inventory xmlns="">
                        <Books>
                            <Book ISBN="0-7356-0562-9" Stock="in" Number="9">
                                <Title>XML in Action</Title>
                                <Summary>XML Web Technology</Summary>
                            </Book>
                            <Book ISBN="0-7356-1370-2" Stock="in" Number="8">
                                <Title>Programming Microsoft Windows With C#</Title>
                                <Summary>C# Programming using the .NET Framework</Summary>
                            </Book>
                            <Book ISBN="0-7356-1288-9" Stock="out" Number="7">
                                <Title>Inside C#</Title>
                                <Summary>C# Language Programming</Summary>
                            </Book>
                            <Book ISBN="0-7356-1377-X" Stock="in" Number="5">
                                <Title>Introducing Microsoft .NET</Title>
                                <Summary>Overview of .NET Technology</Summary>
                            </Book>
                            <Book ISBN="0-7356-1448-2" Stock="out" Number="4">
                                <Title>Microsoft C# Language Specifications</Title>
                                <Summary>The C# language definition</Summary>
                            </Book>
                        </Books>
                        <CDs>
                            <CD Stock="in" Number="3">
                                <Title>Classical Collection</Title>
                                <Summary>Classical Music</Summary>
                            </CD>
                            <CD Stock="out" Number="9">
                                <Title>Jazz Collection</Title>
                                <Summary>Jazz Music</Summary>
                            </CD>
                        </CDs>
                    </Inventory>
                </x:XData>
            </XmlDataProvider>
        </Window.Resources>
        <StackPanel>
            <TextBlock FontSize="18" FontWeight="Bold" Margin="10"
                HorizontalAlignment="Center">XML Data Source Sample</TextBlock>
            <ListBox x:Name="myListBox" Width="400" Height="300" Background="Honeydew">
                <ListBox.ItemsSource>
                    <Binding Source="{StaticResource InventoryData}"
                   XPath="*[@Stock='out'] | *[@Number>=8 or @Number=3]"/>
                </ListBox.ItemsSource>
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock x:Name="txtBlock" FontSize="12" Foreground="Red">
                            <TextBlock.Text>
                                <Binding XPath="Title"/>
                            </TextBlock.Text>
                        </TextBlock>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
            <Button Content="Get TextBlock" Height="40" Width="150" Margin="5" Background="Azure" Click="Button_Click"/>
        </StackPanel>

    C#:

            private childItem FindVisualChild<childItem>(DependencyObject obj)
                where childItem : DependencyObject
            {
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
                {
                    DependencyObject child = VisualTreeHelper.GetChild(obj, i);
                    if (child != null && child is childItem)
                        return (childItem)child;
                    else
                    {
                        childItem childOfChild = FindVisualChild<childItem>(child);
                        if (childOfChild != null)
                            return childOfChild;
                    }
                }
                return null;
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                ListBoxItem myListBoxItem = (ListBoxItem)myListBox.ItemContainerGenerator.ContainerFromItem(myListBox.SelectedItem);
                ContentPresenter myContentPresenter = FindVisualChild<ContentPresenter>(myListBoxItem);
    
                DataTemplate myDataTemplate = myContentPresenter.ContentTemplate;
                TextBlock myTextBlock = (TextBlock)myDataTemplate.FindName("txtBlock", myContentPresenter);
    
                MessageBox.Show("The text of the TextBlock of the selected list item: "
                                    + myTextBlock.Text);
            }

    运行效果图:

    谢谢!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年8月27日 1:39
    版主