2

次のように、XAMLTextのaのプロパティにバインディングを設定しています。TextBlock

<TextBlock x:Name="MyTextBlock" TextWrapping="Wrap" Text="{Binding TextProperty, Converter={StaticResource MyConverter}}"/>

現在使用されているコードビハインド依存からコンバーターを変更したい。コードビハインドからバインディングのコンバーターを取得して設定するにはどうすればよいですか?私は次のような何かが好きです:

if (converter = x)
    converter = y;
else
    converter = x;
4

1 に答える 1

5

バインディング自体を取得する必要があります。

//For WPF:
// var binding = BindingOperations.GetBindingBase(
//     MyTextBlock,
//     TextBlock.TextProperty);

//For SilverLight we have to use the expression:
var expr = MyTextBlock.GetBindingExpression(TextBlock.TextProperty);
if (expr != null)
{
    // for Silverlight we have to use the ParentBinding of the expression
    var binding = expr.ParentBinding;
    binding.Converter = yourLogicHere;

    // in WPF there are 3 types of bindings
    /*
    else if (binding is MultiBinding)
    {
        ((MultiBinding)binding).Converter = yourMultiLogicHere;
    }
    else if (binding is PriorityBinding)
    {
        foreach (var childBinding in ((PriorityBinding)binding).Bindings)
        {
            ((Binding)childBinding).Converter = yourLogicHere;
        }
    }
    */
}
于 2012-09-10T16:46:09.207 に答える