UWP - 将TextBox.Text绑定到Nullable< int> [英] UWP - Bind TextBox.Text to Nullable<int>

查看:320
本文介绍了UWP - 将TextBox.Text绑定到Nullable< int>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它是正确的,它是目前不可能绑定到任何可空< T> 环球XAML应用程序



我发现这个链接,从2013年:



https://social.msdn.microsoft.com/Forums/en-US/befb9603- b8d6-468d-AD36-ef82a9e29749 /文本框文本结合上可空类型论坛= winappswithcsharp



规定:?



绑定到空的值是不是在Windows 8 Store应用程序的支持。它只是没能到这个版本。我们已经有这种行为的错误的v.Next。



但它真的可以,这还没有固定的?



我的绑定:

 <文本框的文本={结合走秀,模式=双向}标题=服务/> 



我的属性:

 公众诠释?供应
{
{返回_serves; ; }

{
_serves =价值;
OnPropertyChanged();
}
}

和我在输出得到错误:

 错误:无法从目标回源保存价值。 
BindingExpression:
路径='供应'
的DataItem ='MyAssembly.MyNamespace.RecipeViewModel,MyAssembly程序,版本= 1.0.0.0,文化=中立,公钥=空';目标元素是'Windows.UI.Xaml.Controls.TextBox'(名称='空');目标属性是'文本'(类型'字符串')。


解决方案

好像它不固定的。由于XAML是使用内置的转换器,在这种情况下,你也许可以用自己的调换,处理nullables:



XAML:



<预类=郎咸平的XML prettyprint-覆盖> < StackPanel的背景={ThemeResource ApplicationPageBackgroundThemeBrush}>
< StackPanel.Resources>
<局部:NullConverter X:键=NullableIntConverter/>
< /StackPanel.Resources>
<文本框的文本={结合走秀,模式=双向,转换器= {StaticResource的NullableIntConverter}}标题=服务/>
< / StackPanel的>



后面的代码:



<预类=郎-cs prettyprint-覆盖> 公共类NullConverter:的IValueConverter
{
公共对象转换(对象的值,类型TARGETTYPE,对象参数,字符串语言)
{返回值; }

公共对象ConvertBack(对象的值,类型TARGETTYPE,对象参数,字符串语言)
{
INT温度;
如果(string.IsNullOrEmpty((字符串)值)|| int.TryParse((串)值,温度出去)!)返回NULL;
否则返回温度;
}
}

公共密封部分类的MainPage:页面,INotifyPropertyChanged的
{
私人诠释? _供应;

公共事件PropertyChangedEventHandler的PropertyChanged;
公共无效RaiseProperty(字符串名称)=> ?的PropertyChanged .Invoke(这一点,新PropertyChangedEventArgs(名));

公众诠释?供应
{
{返回_serves; }
集合{_serves =价值; RaiseProperty(服务); }
}

公众的MainPage()
{
this.InitializeComponent();
的DataContext =这一点;
}
}


Is it correct that it is not currently possible to bind to any Nullable<T> in Universal XAML Apps?

I found this link from 2013:

https://social.msdn.microsoft.com/Forums/en-US/befb9603-b8d6-468d-ad36-ef82a9e29749/textbox-text-binding-on-nullable-types?forum=winappswithcsharp

Stating that:

Binding to nullable values isn't supported in Windows 8 Store apps. It just didn't make it into this release. We've already got bugs on this behavior for v.Next.

But can it really be that this has not been fixed yet?

My Binding:

<TextBox Text="{Binding Serves, Mode=TwoWay}" Header="Serves"/>

My Property:

public int? Serves
{
    get { return _serves; ; }
    set
    {
        _serves = value;
        OnPropertyChanged();
    }
}

And the error I get in my output:

Error: Cannot save value from target back to source. 
BindingExpression: 
    Path='Serves' 
    DataItem='MyAssembly.MyNamespace.RecipeViewModel, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'; target element is 'Windows.UI.Xaml.Controls.TextBox' (Name='null'); target property is 'Text' (type 'String').

解决方案

Seems like it's not fixed. As XAML is using a build-in converter, in this case you can probably exchange it with your own, dealing with nullables:

XAML:

<StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <StackPanel.Resources>
        <local:NullConverter x:Key="NullableIntConverter"/>
    </StackPanel.Resources>
    <TextBox Text="{Binding Serves, Mode=TwoWay, Converter={StaticResource NullableIntConverter}}" Header="Serves"/>
</StackPanel>

Code behind:

public class NullConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    { return value; }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        int temp;
        if (string.IsNullOrEmpty((string)value) || !int.TryParse((string)value, out temp)) return null;
        else return temp;
    }
}

public sealed partial class MainPage : Page, INotifyPropertyChanged
{
    private int? _serves;

    public event PropertyChangedEventHandler PropertyChanged;
    public void RaiseProperty(string name) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));

    public int? Serves
    {
        get { return _serves; }
        set { _serves = value; RaiseProperty("Serves"); }
    }

    public MainPage()
    {
        this.InitializeComponent();
        DataContext = this;
    }
}

这篇关于UWP - 将TextBox.Text绑定到Nullable&lt; int&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆