silverlight中的动态图像源绑定
我想根据 ChildWindow
中的 DataContext
设置图像的源。这是 XAML 文件:
<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>
如果我重写 ChildWindow
的 Show
方法并设置图像的源,它工作正常:
public new void Show()
{
DialogIcon.Source = new BitmapImage(new Uri(@"/Images/DialogWindow/Confirm.png", UriKind.Relative));
base.Show();
}
但它看起来很难看,而且不是“silverlight 方式” ,所以我决定进行更改:
<Image x:Name="DialogIcon" Source="{Binding DialogIconType, Converter={StaticResource DialogIconConverter}}"></Image>
您会看到我注册了一个 DialogIconConverter
来绑定 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));
}
...
}
但它现在不起作用,我在这个控件中有几个其他转换器工作正常。只有这个不行。能帮忙看看问题出在哪里吗?
编辑: DialogIconType
是一个枚举,也是 DialogContext
的属性。 DialogContext
的实例将被分配给 DialogWindow
的 DataContext
属性。
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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可能很愚蠢,但是您是否确保您的转换器在您的 xaml 文件中正确引用?
否则,我建议尝试使用此语法作为 URI 的路径(将图像设置作为资源):
编辑:
好的,我想我已经明白了:
查看输出窗口,您可能会看到一些错误 40 绑定...blablabla...
我的猜测是转换器是正确的,但绑定的源不是,所以基本上转换器甚至没有被使用。
原因是您的 DialogIconType 不是依赖属性,因此无法绑定它。
换句话说,这个:
应该变成这样:
另外,在你的Xaml中,你应该绑定到“IconType”,而不是“DialogIconType”(这是一种类型而不是属性)
(这甚至可能是唯一的问题,因为我不确定这里是否真的需要 dependencyProperty,现在我想到了)
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):
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 :
should become this :
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)
假设
DialogIconType
是图像的路径(例如“Images/DialogWindow/Confirm.png”),它应该在没有值转换器的情况下工作,如下所示:编辑:
返回从 valueconverter 的
Convert
方法到图像的路径也是可能的 - 即:编辑 2:
使用
UriKind.Relative
也可以使用以下内容:Assuming that
DialogIconType
is the path to your image (e.g. "Images/DialogWindow/Confirm.png"), it should work without a valueconverter as shown below:EDIT:
Returning the path to the image from the valueconverter's
Convert
method is also possible - i.e.:EDIT 2:
The following also works using
UriKind.Relative
: