4

私はDataGridと 2を持っていStaticResourceます。

RowStyleDataGrid を 2 つの StaticResources のいずれかにバインドしたいと考えています。

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.

4

2 に答える 2

2

Static Resource キーは、動的に割り当てることができる値ではありません。キーの名前は、Xaml でインライン化する必要があります。

正しいアプローチはこれです: -

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

「MyConverter」キーに対して格納されているコンバーターがStyleオブジェクトを返す場所。ResourceDictionaryタイプのプロパティをコンバーターに追加し、その辞書にスタイルを配置して、コンバーターがルックアップできることに注意してください。

実際、私はすでにこれが可能なコンバーターをここに書いています。

于 2011-01-10T12:43:09.750 に答える
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(); 
    }
}
于 2014-02-13T21:09:11.177 に答える