问题描述
限时送ChatGPT账号..我有一个名为 ItemGrid 的用户控件,带有一个带有图像的按钮,我将图像移动到一个控件模板,这样我就可以正确调整大小:
I have a usercontrol named ItemGrid with a button with an image, I moved the image to a controltemplate so I would be able to size it right:
<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 中,我想根据当前选择的图像(暗/亮)设置正确的图像
In the MainPage I want to set the right image depending on the currently selected them (Dark / Light)
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 移动到控件模板后,UserControls.ItemGrid' 不包含 'imgOrder' 的定义
That gives me: UserControls.ItemGrid' does not contain a definition for 'imgOrder' after I moved imgOrder to the controltemplate
我尝试使用 findname 但这也为 img 提供了空引用异常
I've tried to use findname but that gives a nullreference exception too for 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 中,但似乎根本没有被触发?
I also tried putting the findname in the OnApplyTemplate of the control but that doesn't seem to get fired at all?
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
Image i = this.FindName("imgOrder") as Image;
}
我希望有人对此有答案吗?
I hope somebody has an answer to this?
亲切的问候,
迈克
推荐答案
我认为在这种情况下您可以使用 VisualTreeHelper 类 - WPF 这里有一个关于此的主题,但如果您不想阅读它:
I think in this case you can find it by using the VisualTreeHelper class - there's a thread on this in WPF here, but in case you don't feel like reading it:
制作这个方法:
/// <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;
}
并使用它来查找您的图片:
and use it to find your image:
Image img = FindChild<Image>(btnOrder, "imgOrder");
希望它有效(我承认我还没有测试过这个)......至于为什么 OnApplyTemplate() 不起作用,我相信只有当你将 Button 子类化以制作你自己的自定义按钮时才会调用它.
Hopefully that works (I'll admit I haven't tested this yet)...and as for why OnApplyTemplate() doesn't work, I believe it's only called if you subclass the Button to make your own Custom button.
这篇关于如何在控制模板中引用图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论