C# 获取并设置代码隐藏中的绑定转换器
在我的XAML中,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
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;
}
}
*/
}