如何在controltemplate中引用图像

时间:2012-02-25 12:42:44

标签: silverlight windows-phone-7 xaml

我有一个名为ItemGrid的usercontrol,带有一个带图像的按钮,我将图像移动到一个控件模板,所以我可以正确调整它的大小:

<Button x:Name="btnOrder" Click="btnOrder_Click" HorizontalAlignment="Right" Width="48" Height="48" Margin="0,0,0,100">
    <Button.Template>
        <ControlTemplate>
            <Image x:Name="imgOrder" Source="/Images/dark/appbar.food.png" Stretch="None"/>
        </ControlTemplate>
    </Button.Template>
</Button>

在MainPage中我想根据当前选择的图像设置正确的图像(暗/亮)

private void detecttheme()
    {
        Visibility v = (Visibility)Resources["PhoneLightThemeVisibility"];
        if (v == System.Windows.Visibility.Visible)
        {
            uri = new Uri("/Images/light/appbar.food.png", UriKind.Relative);
            imgSource = new BitmapImage(uri);
            ItemGrid.imgOrder.Source = imgSource;

        }
        else  ....

这让我:在将imgOrder移动到controltemplate之后,UserControls.ItemGrid'不包含'imgOrder'的定义

我尝试使用findname但是对于img

也会给出null引用异常
//Use FindName because we cannot directly reference the image because it's in a control template
  Image img = ItemGrid.FindName("imgOrder") as Image;
  img.Source = imgSource;

我也尝试将findname放在控件的OnApplyTemplate中,但似乎根本没有被触发?

 public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
         Image i = this.FindName("imgOrder") as Image;
    }

我希望有人对此有答案?

亲切的问候,

麦克

1 个答案:

答案 0 :(得分:5)

我认为在这种情况下,您可以使用VisualTreeHelper类 - there's a thread on this in WPF here找到它,但如果您不想阅读它:

制作此方法:

            /// <summary>
/// Finds a Child of a given item in the visual tree. 
/// </summary>
/// <param name="parent">A direct parent of the queried item.</param>
/// <typeparam name="T">The type of the queried item.</typeparam>
/// <param name="childName">x:Name or Name of child. </param>
/// <returns>The first parent item that matches the submitted type parameter. 
/// If not matching item can be found, 
/// a null parent is being returned.</returns>
public static T FindChild<T>(DependencyObject parent, string childName)
   where T : DependencyObject
{    
  // Confirm parent and childName are valid. 
  if (parent == null) return null;

  T foundChild = null;

  int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
  for (int i = 0; i < childrenCount; i++)
  {
    var child = VisualTreeHelper.GetChild(parent, i);
    // If the child is not of the request child type child
    T childType = child as T;
    if (childType == null)
    {
      // recursively drill down the tree
      foundChild = FindChild<T>(child, childName);

      // If the child is found, break so we do not overwrite the found child. 
      if (foundChild != null) break;
    }
    else if (!string.IsNullOrEmpty(childName))
    {
      var frameworkElement = child as FrameworkElement;
      // If the child's name is set for search
      if (frameworkElement != null && frameworkElement.Name == childName)
      {
        // if the child's name is of the request name
        foundChild = (T)child;
        break;
      }
    }
    else
    {
      // child element found.
      foundChild = (T)child;
      break;
    }
  }

  return foundChild;
}

并用它来查找你的图片:

Image img = FindChild<Image>(btnOrder, "imgOrder"); 

希望这有效(我承认我还没有测试过)...至于为什么OnApplyTemplate()不起作用,我相信只有你将Button子类化为自己的自定义按钮才会调用它