Art*_*rov 2 c# data-binding wpf xaml
我需要在XAML中进行一些复杂的绑定.我有一个DependencyProperty
typeof(double)
; 让我们来命名吧SomeProperty
.在我控制的XAML代码中的某个地方,我需要使用整个SomeProperty
值,只有一半,某处SomeProperty/3
,等等.
我该怎么做:
<SomeControl Value="{Binding ElementName=MyControl, Path=SomeProperty} / 3"/>
Run Code Online (Sandbox Code Playgroud)
:)
期待.
使用分部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();
}
}
Run Code Online (Sandbox Code Playgroud)
<!-- 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}"/>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2510 次 |
最近记录: |