Silverlight 如何在controltemplate中引用图像
我有一个名为ItemGrid的用户控件,其中有一个带有图像的按钮,我将图像移动到了一个controltemplate,以便能够正确调整其大小:Silverlight 如何在controltemplate中引用图像,silverlight,windows-phone-7,xaml,Silverlight,Windows Phone 7,Xaml,我有一个名为ItemGrid的用户控件,其中有一个带有图像的按钮,我将图像移动到了一个controltemplate,以便能够正确调整其大小: <Button x:Name="btnOrder" Click="btnOrder_Click" HorizontalAlignment="Right" Width="48" Height="48" Margin="0,0,0,100"> <Button.Template> <ControlTempl
<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>
这就给了我:UserControls.ItemGrid’在我将imgOrder移动到controltemplate后不包含“imgOrder”的定义
我尝试过使用findname,但这也为img提供了一个nullreference异常
//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;
}
我希望有人能回答这个问题
亲切问候,
Mike我认为在这种情况下,您可以使用VisualTreeHelper类找到它,但如果您不想阅读它: 采用以下方法:
/// <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;
}
//
///在可视树中查找给定项的子项。
///
///查询项的直接父项。
///查询项的类型。
///x:孩子的名字或名字。
///与提交的类型参数匹配的第一个父项。
///如果找不到匹配的项目,
///正在返回空的父级。
公共静态T FindChild(DependencyObject父对象,字符串childName)
其中T:DependencyObject
{
//确认父项和子项名称有效。
if(parent==null)返回null;
T foundChild=null;
int childrenCount=visualtreeheloper.GetChildrenCount(父级);
for(int i=0;i
并使用它查找您的图像:
Image img = FindChild<Image>(btnOrder, "imgOrder");
Image img=FindChild(btnOrder,“imgOrder”);
希望它能起作用(我承认我还没有测试过它)…至于为什么OnApplyTemplate()不起作用,我相信只有在您将按钮子类化以创建自己的自定义按钮时才会调用它。我认为在这种情况下,您可以使用VisualTreeHelper类找到它,但如果您不想阅读它: 采用以下方法:
/// <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;
}
//
///在可视树中查找给定项的子项。
///
///查询项的直接父项。
///查询项的类型。
///x:孩子的名字或名字。
///与提交的类型参数匹配的第一个父项。
///如果找不到匹配的项目,
///正在返回空的父级。
公共静态T FindChild(DependencyObject父对象,字符串childName)
其中T:DependencyObject
{
//确认父项和子项名称有效。
if(parent==null)返回null;
T foundChild=null;
int childrenCount=visualtreeheloper.GetChildrenCount(父级);
for(int i=0;i
并使用它查找您的图像:
Image img = FindChild<Image>(btnOrder, "imgOrder");
Image img=FindChild(btnOrder,“imgOrder”);
希望它能起作用(我承认我还没有测试过它)…至于为什么OnApplyTemplate()不起作用,我相信只有当您对按钮进行子类化以创建自己的自定义按钮时,才会调用它