【问题标题】:How to binding a control's property with some compute in XAML (WPF)?如何将控件的属性与 XAML (WPF) 中的某些计算绑定?
【发布时间】:2019-04-24 21:56:35
【问题描述】:

例如,如果我想画一个正方形,每边 50 px,代码如下:<Rectangle Width="50" Height="{Binding Path=Width,RelativeSource={RelativeSource Self}}" Fill="Blue"/>; 但是如果我想让高度总是等于宽度的一半,代码不正确:<Rectangle Width="50" Height="{Binding Path=Width/2,RelativeSource={RelativeSource Self}}" Fill="Blue"/>,那么如何在 XAML 中做到这一点?

【问题讨论】:

标签: c# .net wpf


【解决方案1】:

类似的东西。 后面的代码:

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;
    }
}

还有xaml:

<local:MyConverter x:key="MyConverter"/>



<Rectangle Width="50" Height="{Binding Path=Width,RelativeSource={RelativeSource Self}, Converter={StaticResource MyConverter}}" Fill="Blue"/>

【讨论】:

    猜你喜欢
    • 2011-03-03
    • 1970-01-01
    • 1970-01-01
    • 2019-07-11
    • 2013-05-17
    • 1970-01-01
    • 2023-01-16
    • 2012-09-09
    • 1970-01-01
    相关资源
    最近更新 更多