2012-02-25 66 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