2016-11-02 2 views
0

J'ai ce code:Contrôle d'accès à l'intérieur des fenêtres de contrôle du moyeu 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> 

Mais je ne sais pas comment accéder au texte bloc, je veux changer la couleur de premier plan du texte bloc dans le code derrière .

+0

Vous pouvez ajouter datatriggers à votre textblock de sorte que vous pouvez déclencher des changements de couleur en fonction des propriétés sur votre modèle. –

Répondre

0

XAML:

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

derrière:

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

Des informations plus détaillées vous pouvez consulter à How to access a Control inside the data template in C# Metro UI in the code behind