gpt4 book ai didi

c# - IValueConverter 和模拟数据

转载 作者:行者123 更新时间:2023-11-30 15:58:36 29 4
gpt4 key购买 nike

我正在尝试创建一个 IValueConverter,它接受一个 enum 并吐出一个 URI。转换器确实按预期在运行时工作。然而,XAML Designer 给我一个错误提示:

Object must be the same type as the enum. The type passed in was 'Mocks.WarframeHelper_Model_Enumerations_15_1293735+RelicTypes'; the enum type was 'WarframeHelper.Model.Enumerations+RelicTypes'.

我有一个更简单的模型版本,其中包含我在设计时只需要的属性,但使用的 enum 完全相同(或者至少应该是)。反正有没有这个。

这是 IValueConverter 的代码(我只是掌握了这些东西的窍门,所以如果我做错了什么,请随时纠正我)

public class NameToUriConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if(Enum.IsDefined(typeof(Enumerations.RelicTypes), value))
{
return new Uri("/Assets/RelicIcons/Relic_" + (value).ToString() + ".png", UriKind.Relative);
}
else return new Uri("/Assets/Placeholder.png", UriKind.Relative);
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return value as string;
}
}

这是我用于模拟数据的自定义数据类型:

public class Sample_RelicModel
{
public Uri ImageUri { get; set; }
public bool isVaulted { get; set; }
public Enumerations.RelicFlavors Flavor { get; set; }
public Enumerations.RelicTypes Type { get; set; }
public Enumerations.DropRearity Rearity { get; set; }
public ObservableCollection<Sample_PrimeItem_Component> DropTable { get; set; }
private int count;
public int Count
{
get { return count; }
set
{
if (value >= 0)
{
count = value;
}
else MessageBox.Show("You don't have enough relics", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
public Sample_RelicModel() { }
}

转换器再次在运行时按预期工作,但 XAML 设计人员不喜欢它,因为模拟数据。

最佳答案

在传递给 Enum.IsDefined 之前将 value 转换为字符串,只要枚举的大小写匹配,它应该可以工作。根据https://msdn.microsoft.com/en-us/library/system.enum.isdefined(v=vs.110).aspx

Enum.IsDefined(typeof(Enumerations.RelicTypes), value.ToString())

关于c# - IValueConverter 和模拟数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43022425/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com