1

我的图像在数据模板中,如果 wp 主题很轻,我想做的是更改图像

<DataTemplate x:Key="citiesItemTemplate">
            <StackPanel Grid.Column="1"  VerticalAlignment="Top">               
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition  />
                        <ColumnDefinition  />
                        <ColumnDefinition  />                                              
                    </Grid.ColumnDefinitions>    
                    <Grid.RowDefinitions>                   
                        <RowDefinition/>
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <TextBlock Grid.Column="0" Height="50" Tap="ProgLngGropus_Tap" Text="{Binding Name}" FontSize="26"  Margin="12,-5,12,6"/>                   
                    <ToggleButton Grid.Column="2" x:Name="MyToggleButton" Style="{StaticResource FlipButton}">
                        <ToggleButton.Content>
                            <Image Grid.Column="2" Margin="0,-10,-33,0" Height="40" Width="40" x:Name="ArrowDownImg"  Source="/Images/appbar.dark.arrow.down.circle.rest.png" />                            
                        </ToggleButton.Content>
                    </ToggleButton>
                    <TextBlock TextWrapping="Wrap" Text="{Binding Lang}" Grid.Column="0" Grid.Row="1" x:Name="Desc"
                       Foreground="Orange" Visibility="{Binding ElementName=MyToggleButton,
                        Path=IsChecked, Converter={StaticResource ValueConverterBoolToVis}}">                        
                    </TextBlock>

                </Grid>

            </StackPanel>
        </DataTemplate> 

但我无法访问 ArrowDownImg

Visibility darkBackgroundVisibility = (Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"];


    if (darkBackgroundVisibility != Visibility.Visible)
    {
        //Error in finding ArrowDownImg
     ***//ArrowDownImg.Source = "/Images/appbar.light.arrow.down.circle.rest.png"***
    }
4

2 回答 2

2

你也可以做

Image arrowDownImg = MyObjectUsingTheDataTemplate.FindVisualChild("ArrowDownImg") as Image;
arrowDownImg.Source = ...

通过使用以下扩展方法

    public static FrameworkElement FindVisualChild(this FrameworkElement root, string name)
    {
        FrameworkElement temp = root.FindName(name) as FrameworkElement;
        if (temp != null)
            return temp;

        foreach (FrameworkElement element in root.GetVisualDescendents())
        {
            temp = element.FindName(name) as FrameworkElement;
            if (temp != null)
                return temp;
        }

        return null;
    }

    public static IEnumerable<FrameworkElement> GetVisualDescendents(this FrameworkElement root)
    {
        Queue<IEnumerable<FrameworkElement>> toDo = new Queue<IEnumerable<FrameworkElement>>();

        toDo.Enqueue(root.GetVisualChildren());
        while (toDo.Count > 0)
        {
            IEnumerable<FrameworkElement> children = toDo.Dequeue();
            foreach (FrameworkElement child in children)
            {
                yield return child;
                toDo.Enqueue(child.GetVisualChildren());
            }
        }
    }

    public static IEnumerable<FrameworkElement> GetVisualChildren(this FrameworkElement root)
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(root); i++)
        {
            yield return VisualTreeHelper.GetChild(root, i) as FrameworkElement;
        }
    }
于 2012-10-09T07:00:40.217 回答
1

答:这是我所做的,我添加了加载的事件

 <ToggleButton Grid.Row="0" Grid.Column="2" Margin="0,-10,-33,0" x:Name="MyToggleButton" Style="{StaticResource FlipButton}">
                        <ToggleButton.Content>
                            <Image Loaded="ArrowDownImg_Loaded" Height="50" Width="50" x:Name="ArrowDownImg"   />                               
                        </ToggleButton.Content>
                    </ToggleButton>

在事件中:

  private void ArrowDownImg_Loaded(object sender, RoutedEventArgs e)
        {
            Visibility darkBackgroundVisibility = (Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"];
            var ArrowDownImg = sender as Image;

            if (darkBackgroundVisibility != Visibility.Visible)
            {
                ArrowDownImg.Source = new BitmapImage(new Uri("/Images/appbar.arrow.down.circle.dark.rest.png", UriKind.Relative));
            }
            else
            {
                ArrowDownImg.Source = new BitmapImage(new Uri("/Images/appbar.arrow.down.circle.light.rest.png", UriKind.Relative));
            }
        }
于 2012-10-08T19:56:40.420 回答