Xamarin定制BindableProperty与转换器

时间:2017-12-28 13:57:45

标签: c# xaml xamarin bindable

我有一个带有自定义BindableProperty的自定义视图:

public string Valore {
            get { return (string)GetValue(ValoreProperty); }
            set { SetValue(ValoreProperty, value); }
        }

    public static readonly BindableProperty ValoreProperty =
        BindableProperty.Create(
            propertyName: nameof(Valore),
            returnType: typeof(string),
            declaringType: typeof(VolosTimePickerView),
            defaultBindingMode: BindingMode.TwoWay,
            propertyChanged: ValorePropertyChanged,
            propertyChanging: ValorePropertyChanging
        );

在我的XAML中,由于Valore的值是可空的Int,我必须使用这样的转换器:

<view:VolosTimePickerView Grid.Row="0" Grid.Column="1" 
     Valore="{Binding OrariSelezionati.ViaggioInizioMattina, Converter={StaticResource NullableConverter}}" />

如果不在XAML端使用Converter={StaticResource NullableConverter,而是直接在属性后面的代码中,我怎么能做同样的事情呢?

谢谢!

1 个答案:

答案 0 :(得分:0)

您的Valorestring。尝试将其转换为Nullable<int> Valore。看看会发生什么。

并尝试在ViewModel内绑定Valore的值。所以你会得到

Nullable<int> _Valore;
public Nullable<int> Valore {get; set;}