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;
}
我希望有人有一個答案?
親切的問候,
邁克
大!日Thnx Blakomen1 – 2012-02-27 15:35:51