2016-11-02 1 views
0

Ich habe diesen Code:Zugangskontrolle innerhalb Nabensteuerfenster 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> 

Aber ich weiß nicht, wie Sie den Text-Block zuzugreifen, ich die Vordergrundfarbe des Textblocks in Code ändern hinter .

+0

Sie datatriggers zu Ihrem Textblock hinzufügen können, so dass Sie Farbänderungen basierend auf Eigenschaften auslösen können auf deinem Modell. –

Antwort

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 hinter:

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

Detailliertere Informationen, die Sie How to access a Control inside the data template in C# Metro UI in the code behind beziehen könnte