2012-02-25 59 views
0

我有一个名为ItemGrid与图像的按钮用户控件,我搬到了图像的控件模板,所以我将能够尺寸是正确的:如何参考图像中的ControlTemplate

<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 .... 

这给了我设置:UserControls.ItemGrid”不包含定义‘imgOrder’后,我搬到imgOrder到控件模板

我试着使用findname,但给人的nullreference例外太多了IMG

//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; 
    } 

我希望有人有一个答案?

亲切的问候,

迈克

回答

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()不工作,我相信如果你继承了按钮,使自己这只是叫自定义按钮。

+0

大!日Thnx Blakomen1 – 2012-02-27 15:35:51