我有一个DataGrid
和两个StaticResource
。
我想将DataGrid的RowStyle
绑定到两个StaticResource之一。
RowStyle="{StaticResource {Binding Status, Converter={StaticResource MyConverter}}}"
MyConverter返回StaticResource的密钥。
但是我收到了这个错误:
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
答案 0 :(得分:2)
静态资源键不是可以分配动态的值。密钥的名称需要在Xaml中内联。
正确的做法是: -
RowStyle="{Binding Status, Converter={StaticResource MyConverter}}"
针对“MyConverter”键存储的转换器返回Style
个对象。请注意,您可以向转换器添加类型为ResourceDictionary
的属性,并将样式放在该字典中,以便转换器进行查找。
事实上,我已经编写了一个能够实现此here的转换器。
答案 1 :(得分:0)
// Another version of writing such a converter
public abstract class BaseConverter : MarkupExtension
{
protected IServiceProvider ServiceProvider { get; set; }
public override object ProvideValue(IServiceProvider serviceProvider)
{
ServiceProvider = serviceProvider;
return this;
}
}
public class StaticResourceConverter : BaseConverter, IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return new StaticResourceExtension(value).ProvideValue(ServiceProvider);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
//TODO - implement this for a two-way binding
throw new NotImplementedException();
}
}