C# 在代码中查找textblock以操纵颜色

C# 在代码中查找textblock以操纵颜色,c#,windows-phone-7,C#,Windows Phone 7,我需要在xaml中找到一个特定的控件,以便在代码中进行操作以更改背景 我的问题是,找不到具体的控件 我尝试了.FindByName(Textblock)和visualtreehelper。还试图在代码txtVeranderkleur中键入它,但系统不知道该控件,因为我猜它在childs中。这对我来说完全不管用 我需要找到“txtVeranderkleur”。所以我可以在代码中更改颜色 <Grid x:Name="LayoutRoot" Background="White">

我需要在xaml中找到一个特定的控件,以便在代码中进行操作以更改背景

我的问题是,找不到具体的控件

我尝试了.FindByName(Textblock)和visualtreehelper。还试图在代码txtVeranderkleur中键入它,但系统不知道该控件,因为我猜它在childs中。这对我来说完全不管用

我需要找到“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>

**
**

FindName对于DataTemplate中的元素不起作用

如果必须,可以使用lbDagprogrammaInfo.ItemContainerGenerator.ContainerFromIndex获取包含要修改的txtVeranderkleur的ListBoxItem,并使用VisualTreeHelper.GetChild在可视树中搜索TextBlock

如果可以根据每个项目的DataContext中的数据逻辑确定颜色,则可以将背景绑定到适当的属性,并使用IValueConverter选择颜色

如果只希望基于列表框功能(如选择)更改颜色,则还应考虑使用视觉状态更改颜色

编辑:

下面是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;

不适合我。我找不到我的txtVeranderkleur和你的containerfromindex或getchild。但是谢谢anyway@SigfridMaenhout您必须使用ContainerFromIndex获取ListBoxItem,然后多次执行VisualTreeHelper.GetChild以深入到文本块。在ListBoxItem上调用GetChild将获得一个边框,在Border上调用GetChild将获得一个ContentControl,等等。我知道我理解。谢谢