2013-05-07 77 views
0

我需要找到在XAML特定的控制代码操作来改变背景。查找文本块控制彩色

我的问题是,无法找到具体的控制。

我试图.FindByName(文字块),并与visualtreehelper。还试图在代码txtVeranderkleur中键入它,但系统不知道控制,因为它在孩子们内我猜。没有为我工作。

我需要找到 “txtVeranderkleur”。所以我可以更改代码中的颜色。

<Grid x:Name="LayoutRoot" Background="White"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition Height="*"/> 
      <RowDefinition Height="80"/> 
     </Grid.RowDefinitions> 
     <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="0,0,0,28" Orientation="Horizontal"> 
      <Border Background="#EE2E24" CornerRadius="15,15,15,15" Width="450" Margin="15,15,15,15"> 
       <TextBlock x:Name="Events" TextWrapping="Wrap" Text="Evenementen" Style="{StaticResource subtitle}" Margin="15,15,15,15"/> 
      </Border> 
     </StackPanel> 
     <ListBox Grid.Row="1" Margin="12,-15,0,12" x:Name="lbDagprogrammaInfo" SelectionChanged="lbDagprogrammaInfo_SelectionChanged" > 
       <ListBox.ItemTemplate> 
        <DataTemplate> 
        <StackPanel Orientation="Horizontal" Margin="15,0,0,17"> 
         <Border Width="70" Height="70" BorderBrush="#EE2E24" Background="#EE2E24" BorderThickness="3" CornerRadius="3,3,3,3" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="0,5,0,0"> 
          <TextBlock Width="70" Height="70" Text="{Binding LineTeller}" Style="{StaticResource contentRect}"></TextBlock> 
         </Border> 
         <StackPanel Orientation="Horizontal" Margin="8,0,0,0"> 
          **<TextBlock x:Name="txtVeranderkleur" Style="{StaticResource contentText}"> 
           <Run Text="{Binding LineUur}"></Run> 
           <Run Text="{Binding LineNaam}"></Run> 
          </TextBlock>** 

         </StackPanel> 
        </StackPanel> 
       </DataTemplate> 
       </ListBox.ItemTemplate> 
      </ListBox> 
     <StackPanel Width="480" Height="80" Background="Black" Grid.Row="2"> 
      <Image x:Name="imgSponsor" Source="{Binding LineSponsorFoto}" Height="80" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="3" /> 
     </StackPanel> 
    </Grid> 

回答

2

FindName将不适用于DataTemplate中的元素。

如果您需要,您可以使用lbDagprogrammaInfo.ItemContainerGenerator.ContainerFromIndex获取包含要修改的txtVeranderkleur的ListBoxItem,并使用VisualTreeHelper.GetChild向下搜索TextBlock的可视化树。

如果您可以根据每个项目的DataContext中的数据逻辑确定颜色,则可以将Background绑定到相应的Property并使用IValueConverter来选择颜色。

你也应该考虑使用Visual国家改变颜色,如果你只是在寻找基于列表框的功能来改变颜色,如选择。

编辑:

这里的VisualTreeHelper路径将是什么样子,但你应该找一个更通用的方法的一个片段。

ListBoxItem l = lbDagprogrammaInfo.ItemContainerGenerator.ContainerFromIndex(0) as ListBoxItem; 
Border b = VisualTreeHelper.GetChild(l, 0) as Border; 
ContentControl c = VisualTreeHelper.GetChild(b, 0) as ContentControl; 
ContentPresenter p = VisualTreeHelper.GetChild(c, 0) as ContentPresenter; 
StackPanel s = VisualTreeHelper.GetChild(p, 0) as StackPanel; 
TextBlock t = s.FindName("txtVeranderkleur") as TextBlock; 
+0

不为我工作。我没有找到你的containerfromindex或getchild的我的txtVeranderkleur。但是,无论如何,谢谢 – 2013-05-07 14:28:06

+0

@SigfridMaenhout你必须使用ContainerFromIndex来获取ListBoxItem,然后执行VisualTreeHelper.GetChild多次进入文本块。在ListBoxItem上调用GetChild会得到一个Border,在Border上调用GetChild会为你提供一个ContentControl,等等。 – Murkaeus 2013-05-07 15:45:21

+0

啊哈知道我明白。谢谢。 ;) – 2013-05-08 09:21:20