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 に答える