动态图像源在Silverlight结合 [英] Dynamic image source binding in silverlight

查看:150
本文介绍了动态图像源在Silverlight结合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要根据设定的图像源的的DataContext ChildWindow 。这里是XAML文件:

 <控制:ChildWindow X:类=CEM.Controls.DialogWindow
           的xmlns =htt​​p://schemas.microsoft.com/winfx/2006/xaml/$p$psentation
           的xmlns:X =htt​​p://schemas.microsoft.com/winfx/2006/xaml
           的xmlns:控制=CLR的命名空间:System.Windows.Controls;装配= System.Windows.Controls的标题={结合标题}>
  ...
  <图像X:NAME =DialogIcon>< /图像>
  ...
< /控制:ChildWindow>

如果我重写的显示方法 ChildWindow 键,设置图像的来源它的正常工作>

 公开新空展()
{
    DialogIcon.Source =新的BitmapImage(新的URI(@/图片/ DialogWindow / Confirm.png,UriKind.Relative));
    base.Show();
}

但它看起来丑陋,这不是Silverlight的方式,所以我决定改变:

 <图像X:NAME =DialogIcon来源={结合DialogIconType,转换器= {StaticResource的DialogIconConverter}}>< /图像>

您看,我有一个 DialogIconConverter 登记,从源头绑定的DataContext

 公共类DialogIconConverter:的IValueConverter
{
    公共对象转换(对象的值,类型TARGETTYPE,对象参数,CultureInfo的文化)
    {
       //目前这是一个硬codeD路径
       返回新的BitmapImage(新的URI(@/图片/ DialogWindow / Confirm.png,UriKind.Relative));
    }
    ...
 }

但它不是现在的工作,我在这个控制哪些工作正常其他几个转换器。仅这一项是行不通的。你能帮助找到问题出在哪里?

编辑: DialogIconType 是一个枚举,而且它的 DialogContext 的属性。 DialogContext 的一个实例将被分配到的DataContext DialogWindow

 公开枚举DialogIconType
{
    确认,
    警报,
    错误
}
公共类DialogContext
{
    公共字符串名称{搞定;组; }
    公共字符串内容{搞定;组; }
    公共DialogBu​​ttons按钮{搞定;组; }
    公共DialogIconType IconType {搞定;组; }
}
内部DialogWindow(DialogContext上下文)
{
    的InitializeComponent();
    this.DataContext =背景;
}


解决方案

可能是愚蠢的,但是你要确保你的转换器在您的XAML文件正确引用?

否则,我建议尝试这种语法为您的URI路径(含图片设置为资源):

 返回新的BitmapImage(新的URI(包://应用:,,, /图片/ DialogWindow / Confirm.png,UriKind.Relative));

编辑:

好吧,我想我知道了:
看看你的输出窗口,你可能会看到一些错误40绑定... blablabla ...

我的猜测是,该转换器是正确的,但绑定的来源是不是,所以基本上不会甚至使用了转换器。

原因是,你DialogIconType不是依赖属性,因此它不能被绑定到。

换句话说,这样的:

 公共DialogIconType IconType {搞定;组; }

应该成为这样的:

 公共静态的DependencyProperty IconTypeProperty = DependencyProperty.Register(IconType的typeof(DialogIconType)的typeof(DialogContext));
公共DialogIconType IconType
{
    {返回(DialogIconType)(的GetValue(IconTypeProperty)); }
    集合{的SetValue(IconTypeProperty,值); }
}

加,在你的XAML中,你应该绑定到IconType,而不是DialogIconType(这是一个类型,而不是一个属性)

(这甚至可能是唯一的问题,因为我如果的DependencyProperty其实就是真的需要在这里不知道,现在我想起来了)

I want to set an image's source according to its DataContext in a ChildWindow. Here is the XAML file:

<controls:ChildWindow x:Class="CEM.Controls.DialogWindow"
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
           xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls" Title="{Binding Title}">
  ...
  <Image x:Name="DialogIcon"></Image>
  ...
</controls:ChildWindow>

It's working fine if I override the Show method of the ChildWindow and set the image's source:

public new void Show()
{
    DialogIcon.Source = new BitmapImage(new Uri(@"/Images/DialogWindow/Confirm.png", UriKind.Relative));
    base.Show();
}

But it looks ugly and it's not the "silverlight way", so I decide to change:

<Image x:Name="DialogIcon" Source="{Binding DialogIconType, Converter={StaticResource DialogIconConverter}}"></Image>

You see I have a DialogIconConverter registered to bind the source from the DataContext.

public class DialogIconConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
       //currently it's an hard-coded path
       return new BitmapImage(new Uri(@"/Images/DialogWindow/Confirm.png", UriKind.Relative));
    }
    ...
 }

But it's not working now, I have several other converters in this control which are working fine. Only this one is not working. Can you help to find where the problem is?

EDIT: DialogIconType is an enum, and also it's a property of DialogContext. An instance of DialogContext will be assigned to DataContext property of the DialogWindow.

public enum DialogIconType
{ 
    Confirm,
    Alert,
    Error
}
public class DialogContext
{
    public string Title { get; set; }
    public string Content { get; set; }
    public DialogButtons Buttons { get; set; }
    public DialogIconType IconType { get; set; }
}
internal DialogWindow(DialogContext context)
{
    InitializeComponent();
    this.DataContext = context;
}

解决方案

might be silly, but did you make sure that your converter is referenced properly in your xaml file ?

otherwise, I suggest trying this syntax as path for your URI (with images setup as resources):

return new BitmapImage(new Uri("pack://application:,,,/Images/DialogWindow/Confirm.png", UriKind.Relative));

EDIT :

ok, I think I've got it : look into your output window, you will probably see some error 40 binding ... blablabla...

My guess is that the converter is right, but the source of the binding isn't, so basically the converter is not even used.

The reason is that your DialogIconType is not a dependency property, so it cannot be bound to.

in other words, this :

public DialogIconType IconType { get; set; }

should become this :

public static DependencyProperty IconTypeProperty = DependencyProperty.Register("IconType", typeof(DialogIconType), typeof(DialogContext));
public DialogIconType IconType
{
    get { return (DialogIconType)(GetValue(IconTypeProperty)); }
    set { SetValue(IconTypeProperty , value); }
}

plus, in your Xaml, you should Bind to "IconType", and not "DialogIconType" (which is a type and not a property)

(this might even be the sole issue, as I'm not sure if a dependencyProperty Is actually realy needed here, now that I think of it)

这篇关于动态图像源在Silverlight结合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆