ControlTemplate WPF中的图像源

时间:2010-03-16 10:07:11

标签: wpf

我有一个用于Button控件的ControlTemplate,在ControlTemplate中我有用于在按钮中显示的Image控件,现在我想在runt时设置Image Source,因为我必须复制粘贴ControlTemplate for每个按钮都为新按钮设置新图像。

提前致谢。

4 个答案:

答案 0 :(得分:4)

答案 1 :(得分:0)

一般来说,有两种方法可以在运行时设置图像源(下面的代码示例是伪代码,不会编译):

1)在XAML中使用绑定,其中绑定源将是包含图像源的某个对象的属性(这是Slugster所讨论的场景):

这将是你的对象:

public class ViewModel
{
  public string ImageURI {get;set;}
}

并将由XAML呈现,其中您的按钮带有图像,图像源通过绑定设置:

<Image Source="{Binding Source=ViewModel; Path=ImageURI}"/>

2)通过设置代码隐藏来设置图像源。

这将是您的XAML,其中包含带图像的按钮:

<Image x:Name="theImage"/>

并在代码隐藏中设置该图像的来源:

theImage.Source = new BitmapImage(new Uri("yor image uri"));

答案 2 :(得分:0)

您可以使用GetTemplateChild(string childName)方法(使用XAML中定义的元素名称)从模板内部访问项目,例如 - 如果您的图像定义如下:

<Image x:Name="MyImage" Stretch="Fill" />

然后你会这样调用这个方法:

Image myImage = GetTemplateChild("MyImage") as Image;

if (myImage != null)
{
    myImage.Source = "/Images/MyPicture.jpg";
}

注意:在为控件调用 AFTER OnApplyTemplate之前,您将无法使用此方法。

答案 3 :(得分:0)

<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
}