2013-05-07 76 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