silverlight中的动态图像源绑定

发布于 2024-10-02 23:10:55 字数 2050 浏览 8 评论 0原文

我想根据 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>

如果我重写 ChildWindowShow 方法并设置图像的源,它工作正常:

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 的实例将被分配给 DialogWindowDataContext 属性。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

那小子欠揍 2024-10-09 23:10:55

可能很愚蠢,但是您是否确保您的转换器在您的 xaml 文件中正确引用?

否则,我建议尝试使用此语法作为 URI 的路径(将图像设置作为资源):

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

编辑:

好的,我想我已经明白了:
查看输出窗口,您可能会看到一些错误 40 绑定...blablabla...

我的猜测是转换器是正确的,但绑定的源不是,所以基本上转换器甚至没有被使用。

原因是您的 DialogIconType 不是依赖属性,因此无法绑定它。

换句话说,这个:

public DialogIconType IconType { get; set; }

应该变成这样:

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

另外,在你的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):

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)

屋顶上的小猫咪 2024-10-09 23:10:55

假设 DialogIconType 是图像的路径(例如“Images/DialogWindow/Confirm.png”),它应该在没有值转换器的情况下工作,如下所示:

<Image Source="{Binding DialogIconType}" />

编辑:

返回从 valueconverter 的 Convert 方法到图像的路径也是可能的 - 即:

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    return "Images/DialogWindow/Confirm.png";
}

编辑 2:

使用 UriKind.Relative 也可以使用以下内容:

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    return new BitmapImage(new Uri("Images/DialogWindow/Confirm.png", 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:

<Image Source="{Binding DialogIconType}" />

EDIT:

Returning the path to the image from the valueconverter's Convert method is also possible - i.e.:

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    return "Images/DialogWindow/Confirm.png";
}

EDIT 2:

The following also works using UriKind.Relative:

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    return new BitmapImage(new Uri("Images/DialogWindow/Confirm.png", UriKind.Relative));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文