[英]How to binding a control's property with some compute in XAML (WPF)?
for example , if I want to draw a square, per side 50 px ,the code like this : <Rectangle Width="50" Height="{Binding Path=Width,RelativeSource={RelativeSource Self}}" Fill="Blue"/>
; 例如,如果我要绘制一个正方形,每边50 px,则代码如下:
<Rectangle Width="50" Height="{Binding Path=Width,RelativeSource={RelativeSource Self}}" Fill="Blue"/>
; but if I want to let the height always equals to half of width , the code is not correct : <Rectangle Width="50" Height="{Binding Path=Width/2,RelativeSource={RelativeSource Self}}" Fill="Blue"/>
, so how to do it in XAML ? 但是如果我想让高度始终等于宽度的一半,则代码是不正确的:
<Rectangle Width="50" Height="{Binding Path=Width/2,RelativeSource={RelativeSource Self}}" Fill="Blue"/>
,那么如何在XAML中做到这一点?
Something like this. 这样的事情。 Code behind:
后面的代码:
public class MyConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return ((double)value)/2;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
}
And xaml: 和xaml:
<local:MyConverter x:key="MyConverter"/>
<Rectangle Width="50" Height="{Binding Path=Width,RelativeSource={RelativeSource Self}, Converter={StaticResource MyConverter}}" Fill="Blue"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.