簡體   English   中英

更改XAML中的綁定值

[英]Change Binding value in XAML

我需要在XAML中進行一些復雜的綁定。 我有一個DependencyProperty typeof(double) ; 讓我們將其命名為SomeProperty 在我的控件的XAML代碼中的某個位置,我需要使用整個SomeProperty值,僅使用一半,某個位置使用SomeProperty/3 ,依此類推。

我該怎么做:

<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}"/>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM