- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试创建一个 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/
我正在尝试将枚举转换为画笔,以便可以使用枚举变量来控制某些控件的颜色 我的枚举(并不真正相关): public enum Colors { Red, Blue, } 这是我的转
我正在使用 .NET 3.5 我有一个 DataGridTextColumn,我想在该列的值为 false 时将背景颜色变为红色。我已经在 XMAL 中看到了这一点,但无法弄清楚如何在代码隐藏中做到这
我有一个 Enum 需要显示在 ComboBox 中。我已经设法使用 ItemsSource 将枚举值获取到组合框,并且我正在尝试将它们本地化。我认为这可以使用值转换器来完成,但由于我的枚举值已经是字
在谷歌上搜索这个问题几个小时,但看不出我哪里出错了。 我有以下转换器,它只返回 Brushes.Red(也尝试过 Colors.Red),但仍然没有成功。 public class ColorConv
我正在尝试创建一个 IValueConverter,它接受一个 enum 并吐出一个 URI。转换器确实按预期在运行时工作。然而,XAML Designer 给我一个错误提示: Object must
我有一个简单的 IValueConverter,它只使用 TypeConverter 进行转换。但是,在某些情况下,提供的 TypeConverter 会失败。 如果转换器未提供 Binding,我想
我有一个具有 3 种通信状态的应用程序:已连接、已断开连接和待处理。通信状态由一些其他参数控制。我想在 IValueConverter 控制的屏幕上显示相应的图像。但我无法让它工作。 这是我包含 3
我有一个 ListView,它有一个包含两列和许多行的 Grid。每行在每一列中都有一个 TextBlock,每个 Text 属性都绑定(bind)到 ListView 的 ItemSource 中的
我有一个包含大约 12 个项目的解决方案,其中一个设置为启动项目,其中包含主窗口。目前我所有的 IValueConverter 都在这个项目中。 我现在想将它们移动到一个单独的项目中,该项目将只包含转
IValueConverter 的最佳实践是什么? : 可以将 Exception 放在 Convert 方法中还是应该返回“something”? 这是一个例子: [ValueConversion(
我正在使用自定义 DateTimeToString :IValueConverter 在我的 ConvertBack 方法中,当转换失败时我抛出异常,但是它没有显示为验证失败(这是一个未处理的应用程序
我有一个异步方法,我想在 IValueConverter 中触发它。 有没有比通过调用 Result 属性强制它同步更好的方法? public async Task Convert(object va
我有一个进度条,我想根据 bool 值更改颜色; true 为绿色,false 为红色。我的代码看起来应该可以工作(当我将它绑定(bind)到文本框时它返回正确的值),但当它是进度条的颜色属性时却不行
我正在尝试使用 IValueConverter 将集合转换为代理对象以进行数据绑定(bind)。 转换器似乎工作正常,但问题是在集合中添加或删除新对象时。 View 中没有刷新相同的.. 模型对象:
我为自定义控件创建了一个 .cs 类,其中包含以下属性: //Property which is defining the unit of the textblock in the Ringsli
有谁知道是否可以对基于 IValueConverter 的类进行数据绑定(bind)? 我有以下转换器: [ValueConversion(typeof(int), typeof(Article))]
好吧,(在写了几次之后)发现System.Windows.Controls命名空间中已经有一个BooleanToVisibilityConverter,这真是一个惊喜。 可能还有更多这样隐藏的节省时间
我有这个代码: namespace Test { public partial class SearchField : UserControl { public Sea
我需要在转换器类中定义 DependencyProperty,因为我需要此数据来进行转换,并且此数据位于另一个对象中,而不是我绑定(bind)到的对象中。 我的转换器类如下: public class
我有这个代码: namespace Test { public partial class SearchField : UserControl { public Sea
我是一名优秀的程序员,十分优秀!