2011-04-19 111 views
2

我需要在XAML中進行一些複雜的綁定。我有一個DependencyPropertytypeof(double);我們將其命名爲SomeProperty。在我的控制XAML代碼中,我需要使用整個值SomeProperty,某處只有一半,某處SomeProperty/3等等。更改XAML中的綁定值

我怎麼可以這樣做:

<SomeControl Value="{Binding ElementName=MyControl, Path=SomeProperty}/3"/> 

:)

期待。

回答

7

使用部門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}"/> 
+0

非常感謝!有用 :) – 2011-04-19 02:52:52