更改XAML中的绑定值
问题描述:
我需要在XAML中进行一些复杂的绑定。我有一个DependencyProperty
typeof(double)
;我们将其命名为SomeProperty
。在我的控制XAML代码中,我需要使用整个值SomeProperty
,某处只有一半,某处SomeProperty/3
等等。更改XAML中的绑定值
我怎么可以这样做:
<SomeControl Value="{Binding ElementName=MyControl, Path=SomeProperty}/3"/>
:)
期待。
答
使用部门ValueConverter
:
public class DivisionConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
int divideBy = int.Parse(parameter as string);
double input = (double)value;
return input/divideBy;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
}
<!-- Created as resource -->
<local:DivisionConverter x:Key="DivisionConverter"/>
<!-- Usage Example -->
<TextBlock Text="{Binding SomeProperty, Converter={StaticResource DivisionConverter}, ConverterParameter=1}"/>
<TextBlock Text="{Binding SomeProperty, Converter={StaticResource DivisionConverter}, ConverterParameter=2}"/>
<TextBlock Text="{Binding SomeProperty, Converter={StaticResource DivisionConverter}, ConverterParameter=3}"/>
非常感谢!有用 :) – 2011-04-19 02:52:52