简体   繁体   English

XAML TwoWay绑定到Nullable类型

[英]XAML TwoWay binding to Nullable types

Environment: WinRt / XAML / C# 环境:WinRt / XAML / C#

I am trying to do a two way binding for a float type property. 我正在尝试对float类型的属性进行两种方式的绑定。 However if that is a nullable type (float? or Nullable) the binding doesnt work. 但是,如果那是可为null的类型(float?或Nullable),则绑定将不起作用。 I have event applied converters for this, and it still doesnt show any sign of bindability. 我为此应用了事件转换器,它仍然没有显示任何可绑定性的迹象。

C#: ViewModel C#:ViewModel

public class MyViewModel : INotifyPropertyChanged
{
    private float _quantity1;
    public float Quantity1
    {
        get
        {
            return this._quantity1;
        }
        set
        {
            this._quantity1 = value;
            RaisePropertyChanged("Quantity1");
        }
    }

    private float? _quantity2;
    public float? Quantity2
    {
        get
        {
            return this._quantity2;
        }
        set
        {
            this._quantity2 = value;
            RaisePropertyChanged("Quantity2");
        }
    }

    private Nullable<float> _quantity3;
    public Nullable<float> Quantity3
    {
        get
        {
            return this._quantity3;
        }
        set
        {
            this._quantity3 = value;
            RaisePropertyChanged("Quantity3");
        }
    }

    public MyViewModel()
    {
        this.Quantity1 = 100.01F;
        this.Quantity2 = 200.02F;
        this.Quantity3 = 300.03F;
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void RaisePropertyChanged(string propertyName)
    {
        System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
        if ((propertyChanged != null))
        {
            propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
        }
    }
}

C# : Converter: C#:转换器:

public sealed class NullableFloatConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        if (value == null)
            return 0F;
        else
            return value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        if (value != null)
            return value;
        else
            return 0;
    }
}

XAML: XAML:

<Page
x:Class="Test_Binding.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Test_Binding"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Page.Resources>
    <local:NullableFloatConverter x:Key="nullConverter" />
</Page.Resources>
    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
    <StackPanel>
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="Quantity1: " Width="150" />
            <TextBox Text="{Binding Quantity1, Mode=TwoWay}" />
            <TextBlock Text="{Binding Quantity1}" />
        </StackPanel>
        <!-- the second text block doesnt get an updated value -->
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="Quantity2: " Width="150"/>
            <TextBox Text="{Binding Quantity2, Mode=TwoWay, Converter={StaticResource nullConverter}}" />
            <TextBlock Text="{Binding Quantity2, Converter={StaticResource nullConverter}}" />
        </StackPanel>
        <!-- the second text block doesnt get an updated value -->
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="Quantity2 (No Converter): " Width="150"/>
            <TextBox Text="{Binding Quantity2, Mode=TwoWay}" />
            <TextBlock Text="{Binding Quantity2}" />
        </StackPanel>
        <!-- the second text block doesnt get an updated value -->
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="Quantity3: " Width="150"/>
            <TextBox Text="{Binding Quantity3, Mode=TwoWay}" />
            <TextBlock Text="{Binding Quantity3}" />
        </StackPanel>
    </StackPanel>
</Grid>

Only the First text block gets updated (ie for Quantity1). 仅“第一个”文本块被更新(例如,Quantity1)。 I cant get the others (Quantity2 & Quantity3) to get updated. 我无法获取其他(Quantity2和Quantity3)进行更新。 Any suggestions ? 有什么建议么 ?

I think the convertBack method wasnt upto scratch, which i got from stackoverflow.com/questions/15406336/databind-a-nullable-type-in-xaml-windows-8-store-app 我认为convertBack方法并没有从头开始,这是我从stackoverflow.com/questions/15406336/databind-a-nullable-type-in​​-xaml-windows-8-store-app中获得的

public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        string s = value as string;

        float result;
        if (!string.IsNullOrWhiteSpace(s) && float.TryParse(s, out result))
        {
            return result;
        }

        return null;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM