Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取并设置代码隐藏中的绑定转换器_C#_Silverlight_Xaml_Windows Phone 7_Windows Phone 8 - Fatal编程技术网

C# 获取并设置代码隐藏中的绑定转换器

C# 获取并设置代码隐藏中的绑定转换器,c#,silverlight,xaml,windows-phone-7,windows-phone-8,C#,Silverlight,Xaml,Windows Phone 7,Windows Phone 8,在我的XAML中,TextBlock的Text属性有一个绑定集,如下所示: <TextBlock x:Name="MyTextBlock" TextWrapping="Wrap" Text="{Binding TextProperty, Converter={StaticResource MyConverter}}"/> 您需要获取绑定本身: //For WPF: // var binding = BindingOperations.GetBindingBase( // M

在我的XAML中,
TextBlock
Text
属性有一个绑定集,如下所示:

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

您需要获取绑定本身:

//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;
        }
    }
    */
}

BindingOperations
没有
GetBindingBase
方法,它只显示
SetBinding
。它说,
“System.Windows.Data.BindingOperations”不包含“GetBindingBase”的定义。
噢,Silverlight…我会更新的。Silverlight是二等公民,没有WPF那么受支持。我会避免的。
//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;
        }
    }
    */
}