繁体   English   中英

集线器控制窗口8.1中的访问控制

[英]Access control inside hub control windows 8.1

我有以下代码:

<HubSection x:Name="MyHub">
    <DataTemplate>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="*"></RowDefinition>
                <RowDefinition Height="200"></RowDefinition>
            </Grid.RowDefinitions>
            <Image x:Name="MYImage" Source="{Binding image}" Height="50" Width="60" VerticalAlignment="Center"/>
             <StackPanel Orientation="Vertical">
                <TextBlock TextWrapping="Wrap" Width="200" VerticalAlignment="Center" HorizontalAlignment="Left" Text="{Binding name}"/>
                <Button Content="Click me">
            </StackPanel>
        </Grid>
    </DataTemplate>
</HubSection>

但是我不知道如何访问文本块,我想在后面的代码中更改文本块的前景色。

XAML:

<Hub x:Name="myHub">
        <HubSection x:Name="myHubSection">
            <DataTemplate>
                <TextBlock x:Name="textbox1" Text="text" Width="300" Height="100">
                </TextBlock>
            </DataTemplate>
        </HubSection>
    </Hub>

后面的代码:

 private void Page_Loaded(object sender, RoutedEventArgs e)
    {
        var hub_section = FindVisualChildByName<HubSection>(this.myHub, "myHubSection");
        var text_box = FindVisualChildByName<WebView>(hub_section, "textbox1");
    }

    public static T FindVisualChildByName<T>(DependencyObject parent, string name)where T : DependencyObject

    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
        {
            var child = VisualTreeHelper.GetChild(parent, i);
            string controlName = child.GetValue(Control.NameProperty) as string;
            if (controlName == name)
            {
                return child as T;
            }
            else
            {
                T result = FindVisualChildByName<T>(child, name);
                if (result != null)
                    return result;
            }
        }
        return null;
    }

您可以在后面的代码中参考如何在C#Metro UI中的数据模板内部访问控件的更多详细信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM