这是我想用多转换器实现的伪代码。
IF vm.AvatarFilePath IS NOT NULL THEN
Image.Source = {Binding AvatarPath}
ELSE
If vm.Gender == {x:Static vm:Gender.Female} THEN
Image.Source = {StaticResource Img_Female}
ELSE
Image.Source = {StaticResource Img_Male}
ENDIF
ENDIF
请注意,我不相信多转换器是正确的方法,我发布了一个尝试solve this problem with DataTriggers here的类似问题。
我尝试使用MultiConverter实现(下面)有问题:
如何开始清理此转换器代码和绑定?
干杯,
Berryl
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
values.ThrowIfNull("values");
foreach (var value in values) {
if (value != null && value.ToString().Equals("{DependencyProperty.UnsetValue}")) return Binding.DoNothing;
}
values[1].ThrowIfNull("gender (value[1])");
var avatarPath = values[0] as string;
BitmapSource result;
if (string.IsNullOrWhiteSpace(avatarPath)) {
var gender = (Gender) values[1];
object bitmapSource;
switch (gender)
{
case Gender.Female:
bitmapSource = DomainSubjects.Img_Female;
break;
default:
bitmapSource = DomainSubjects.Img_Male;
break;
}
result = BitmapHelper.GetBitmapSource(bitmapSource);
}
else {
var bi = new BitmapImage();
bi.BeginInit();
bi.UriSource = new Uri(avatarPath, UriKind.RelativeOrAbsolute);
bi.EndInit();
result = bi;
}
return result;
}
public static BitmapSource GetBitmapSource(object source)
{
BitmapSource bitmapSource = null;
if (source is Icon)
{
var icon = source as Icon;
// For icons we must create a new BitmapFrame from the icon data stream
// The approach we use for bitmaps (below) doesn't work when setting the
// Icon property of a window (although it will work for other Icons)
//
using (var iconStream = new MemoryStream())
{
icon.Save(iconStream);
iconStream.Seek(0, SeekOrigin.Begin);
bitmapSource = BitmapFrame.Create(iconStream);
}
}
else if (source is Bitmap)
{
var bitmap = source as Bitmap;
var bitmapHandle = bitmap.GetHbitmap();
bitmapSource = Imaging
.CreateBitmapSourceFromHBitmap(bitmapHandle, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
bitmapSource.Freeze();
DeleteObject(bitmapHandle);
}
return bitmapSource;
}
<Image Grid.Column="1" Grid.Row="4"
HorizontalAlignment="Center" Margin="10, 0" Width="96" Height="96" Stretch="UniformToFill"
>
<Image.Source>
<MultiBinding Converter="{StaticResource avatarPathConv}">
<Binding Path="AvatarPath"/>
<Binding Path="Gender"/>
</MultiBinding>
</Image.Source>
</Image>
[Test]
public void Convert_AvatarPath_IfBadString_DefaultImage() {
var conv = new AvatarPathConverter();
var result = conv.Convert(new object[] {"blah", Gender.Male}, null, null, CultureInfo.InvariantCulture);
Assert.That(result, Is.EqualTo(DomainSubjects.Img_Male));
}