【发布时间】:2015-06-23 12:18:06
【问题描述】:
是否可以使用附加属性作为多重绑定的参数?怎么样?
我需要使用附加属性的值来更改绑定到 TextBlock 的文本。
我的代码是这样的:
文本块绑定
<TextBlock x:Name="myTxt"
wpfApplication2:TextBlockAttachedProperties.MyProperty="true">
<TextBlock.Text>
<MultiBinding Converter="{StaticResource CustomConverter}" Mode="TwoWay" NotifyOnValidationError="true">
<Binding Path="Test"/>
<Binding ElementName="myTxt" Path="MyProperty" Mode="OneWay"/>
</MultiBinding>
</TextBlock.Text>
我的转换器:
转换器代码
public class CustomConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
double? value = null;
value = values[0] as double?;
DependencyProperty myProperty= null;
if (values.Count() > 1 && values[1] != DependencyProperty.UnsetValue)
//Do something
if (myProperty!= null )
{
//here do something with the value using the attached property
var convertedValue = value;
return convertedValue;
}
return value;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
object[] values = { value };
return values;
}
}
但是,由于我不知道如何将附加属性作为 Multibinding 的参数传递,因此转换器总是获取 DependencyProperty.UnsetValue。
【问题讨论】: