我有一個ControlTemplate是用於按鈕控件,在ControlTemplate中我有用於顯示在按鈕中的圖像控件,現在我想設置圖像源在小時候,因爲我必須複製粘貼每個按鈕的ControlTemplate爲新按鈕設置新圖像。在ControlTemplate中的圖像源WPF
在此先感謝。
我有一個ControlTemplate是用於按鈕控件,在ControlTemplate中我有用於顯示在按鈕中的圖像控件,現在我想設置圖像源在小時候,因爲我必須複製粘貼每個按鈕的ControlTemplate爲新按鈕設置新圖像。在ControlTemplate中的圖像源WPF
在此先感謝。
也許這是你在找什麼:
希望這有助於。
問候,
米希爾Gokani
一般來說有可以設置在運行時的圖像源兩種方式(下面的代碼示例是用僞代碼,並且不會編譯):
1)在XAML使用的,其中源結合結合將是一個包含圖像源(這是該方案Slugster在談論)一些對象的屬性:
這將是你的對象:
public class ViewModel
{
public string ImageURI {get;set;}
}
,並通過XAML,你有你的按鈕呈現圖像和圖像源是se通過結合T:
<Image Source="{Binding Source=ViewModel; Path=ImageURI}"/>
2)通過設置從代碼隱藏圖像源。
這將是您的XAML,你有按鈕,圖像:
<Image x:Name="theImage"/>
,並在代碼隱藏您設置圖像的來源:
theImage.Source = new BitmapImage(new Uri("yor image uri"));
在我的情況下放置在ControlTemplate中的圖像控件和我必須分配圖像控件的源代碼,現在我將它分配到ControlTemplate中,並且我爲每個必須應用樣式的按鈕創建了ControlTemplate。我的問題是,我想爲所有按鈕控件創建一個controlTemplate,並且在我想分配的ControlTemplate中創建,而不是在controlTemplate中。 希望你明白我的觀點 – 2010-03-17 04:11:07
您可以訪問一個項目從模板內部通過使用GetTemplateChild(string childName)
方法(使用XAML中定義的元素名稱),例如 - 如果您的圖像是這樣定義的:
<Image x:Name="MyImage" Stretch="Fill" />
那麼你會調用這個方法是這樣的:
Image myImage = GetTemplateChild("MyImage") as Image;
if (myImage != null)
{
myImage.Source = "/Images/MyPicture.jpg";
}
注意:您將無法使用此方法,直到後OnApplyTemplate
一直呼籲控制。
<Button x:Class="FunitureCtlLib.PressedImageButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="uc"><!--MinHeight="25" MinWidth="50"-->
<Button.Template>
<ControlTemplate>
<Grid>
<Image Name="imgDefault" Source="{Binding Path=DefaultImageSource,ElementName=uc}" Stretch="{Binding Path=ImageStretch,ElementName=uc}"></Image>
<ContentPresenter Content="{TemplateBinding Property=ContentControl.Content}" />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="Button.IsPressed" Value="True">
<Setter Property="Image.Source" TargetName="imgDefault" Value="{Binding Path=PressedImageSource,ElementName=uc}"></Setter>
<Setter Property="UIElement.Effect">
<Setter.Value>
<DropShadowEffect BlurRadius="10" Color="Black" Direction="0" Opacity="0.6" RenderingBias="Performance" ShadowDepth="0" />
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="Button.IsMouseOver" Value="True">
<Setter Property="UIElement.Effect">
<Setter.Value>
<DropShadowEffect BlurRadius="10" Color="White" Direction="0" Opacity="0.6" RenderingBias="Performance" ShadowDepth="0" />
</Setter.Value>
</Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Button.Template>
/// <summary>
/// ImageButton.xaml
/// </summary>
public partial class PressedImageButton : Button
{
#region dependency property
public static readonly DependencyProperty DefaultImageSourceProperty = DependencyProperty.Register("DefaultImageSource", typeof(ImageSource), typeof(PressedImageButton), new PropertyMetadata(null, new PropertyChangedCallback(DefaultImageSourceChangedCallback)));
public static readonly DependencyProperty PressedImageSourceProperty = DependencyProperty.Register("PressedImageSource", typeof(ImageSource), typeof(PressedImageButton), new PropertyMetadata(null, new PropertyChangedCallback(PressedImageSourceChangedCallback)));
public static readonly DependencyProperty ImageStretchProperty = DependencyProperty.Register("ImageStretch", typeof(Stretch), typeof(PressedImageButton), new PropertyMetadata(Stretch.None, new PropertyChangedCallback(ImageStretchChangedCallback)));
#endregion
#region callback
private static void DefaultImageSourceChangedCallback(object sender, DependencyPropertyChangedEventArgs e)
{
if (sender != null && sender is PressedImageButton)
{
PressedImageButton imgbtn = sender as PressedImageButton;
imgbtn.OnDefaultImageSourceChanged(e.OldValue, e.NewValue);
}
}
private static void PressedImageSourceChangedCallback(object sender, DependencyPropertyChangedEventArgs e)
{
if (sender != null && sender is PressedImageButton)
{
PressedImageButton imgbtn = sender as PressedImageButton;
imgbtn.OnPressedImageSourceChanged(e.OldValue, e.NewValue);
}
}
private static void ImageStretchChangedCallback(object sender, DependencyPropertyChangedEventArgs e)
{
if (sender != null && sender is PressedImageButton)
{
PressedImageButton imgbtn = sender as PressedImageButton;
imgbtn.OnImageStretchChanged(e.OldValue, e.NewValue);
}
}
#endregion
#region public property
/// <summary>
///
/// </summary>
public ImageSource DefaultImageSource
{
get
{
return this.GetValue(DefaultImageSourceProperty) as ImageSource;
}
set
{
this.SetValue(DefaultImageSourceProperty, value);
}
}
/// <summary>
///
/// </summary>
public ImageSource PressedImageSource
{
get
{
return this.GetValue(PressedImageSourceProperty) as ImageSource;
}
set
{
this.SetValue(PressedImageSourceProperty, value);
}
}
/// <summary>
///
/// </summary>
public Stretch ImageStretch
{
get
{
return (Stretch)this.GetValue(ImageStretchProperty);
}
set
{
this.SetValue(ImageStretchProperty, value);
}
}
#endregion
#region protected method
protected void OnDefaultImageSourceChanged(object oldValue, object newValue)
{
//viewmodel.DefaultImageSource = newValue as ImageSource;
this.DefaultImageSource = newValue as ImageSource;
}
protected void OnPressedImageSourceChanged(object oldValue, object newValue)
{
//viewmodel.PressedImageSource = newValue as ImageSource;
this.PressedImageSource = newValue as ImageSource;
}
protected void OnImageStretchChanged(object oldValue, object newValue)
{
//viewmodel.ImageStretch = (Stretch)newValue;
this.ImageStretch = (Stretch)newValue;
}
#endregion
#region construct
public PressedImageButton()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(PressedImageButton_Loaded);
}
#endregion
#region private event
void PressedImageButton_Loaded(object sender, RoutedEventArgs e)
{
}
#endregion
}
爲什麼你不能只是ImageSource的屬性,屬性綁定您的視圖模型中? – slugster 2010-03-16 11:28:53
你能解釋一下你的答案嗎? – 2010-03-16 11:39:51