我想根据它DataContext
在 a中设置图像的来源ChildWindow
。这是 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>
如果我覆盖的Show
方法ChildWindow
并设置图像的源,它工作正常:
public new void Show()
{
DialogIcon.Source = new BitmapImage(new Uri(@"/Images/DialogWindow/Confirm.png", UriKind.Relative));
base.Show();
}
但它看起来很丑,而且不是“银光方式”,所以我决定改变:
<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
将分配给 的DataContext
属性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;
}