使用转换器绑定到静态资源

发布于 2024-10-11 20:23:15 字数 361 浏览 5 评论 0原文

我有一个 DataGrid 和两个 StaticResource

我想将 DataGrid 的 RowStyle 绑定到两个 StaticResources 之一。

RowStyle="{StaticResource {Binding Status, Converter={StaticResource MyConverter}}}"

MyConverter 返回 StaticResource 的 Key。

但我收到此错误:

尝试读取或写入受保护的内存。这通常表明其他内存已损坏。

I have a DataGrid and two StaticResource.

I want to bind RowStyle of DataGrid to one of two StaticResources.

RowStyle="{StaticResource {Binding Status, Converter={StaticResource MyConverter}}}"

MyConverter returns StaticResource's Key.

But I get this error:

Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

过潦 2024-10-18 20:23:15

静态资源键不是可以动态分配的值。键的名称需要内联到 Xaml 中。

正确的方法是这样的: -

RowStyle="{Binding Status, Converter={StaticResource MyConverter}}" 

针对“MyConverter”键存储的转换器返回一个 Style 对象。请注意,您可以向转换器添加 ResourceDictionary 类型的属性,并将样式放置在该字典中以供转换器查找。

事实上,我已经编写了一个能够实现此功能的转换器

The Static Resource key is not a value that can be assigned Dynamically. The name of the key needs to inline in the Xaml.

The correct approach is this:-

RowStyle="{Binding Status, Converter={StaticResource MyConverter}}" 

Where the converter that is stored against the "MyConverter" key returns a Style object. Note you could add a property of type ResourceDictionary to you converter and place you styles in that dictionary for you converter to lookup.

In fact I have already written a converter capable of this here.

很糊涂小朋友 2024-10-18 20:23:15
// 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(); 
    }
}
// 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(); 
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文