【问题标题】:UWP - Bind TextBox.Text to Nullable<int>UWP - 将 TextBox.Text 绑定到 Nullable<int>
【发布时间】:2015-10-31 16:10:54
【问题描述】:

目前无法绑定到通用 XAML 应用程序中的任何 Nullable&lt;T&gt; 是否正确?

我从 2013 年找到了这个链接:

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

声明:

Windows 8 应用商店应用不支持绑定到可为空的值。它只是没有进入这个版本。我们已经为 v.Next 发现了这种行为的错误。

但这真的是还没解决吗?

我的绑定:

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

我的财产:

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

我在输出中遇到的错误:

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').

【问题讨论】:

    标签: c# data-binding uwp


    【解决方案1】:

    好像还没有解决。由于 XAML 使用的是内置转换器,在这种情况下,您可以将其与您自己的转换器进行交换,处理可空值:

    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>
    

    后面的代码:

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

    【讨论】:

    • 是的,这也是我的解决方法。我只是有点惊讶这还没有实现。
    • @TroelsLarsen 我猜他们只是因为更重要的错误而超载,因为这个错误很容易被绕过。 Similar bug,可以使用转换器轻松“修复”。
    • @Rommasz:是的。由于 WPF 和 UWP 的相似之处,这样的省略更加突出。无论如何,感谢您的帮助!
    • 截至 2019 年仍未修复。疯狂的是我一开始没有任何问题。然后我将项目的目标版本更改为最新版本的 Win10(最初它针对的是旧版本,但不是 那个 旧的),突然每个 Nullable int 绑定都抛出“没有隐式转换对象和可为空的 int" 错误...
    • 嗨@Romasz,我实现了你的解决方案,但我得到了同样的错误。为了解决它,我改变了方法 Convert 像这样public object Convert(object value, Type targetType, object parameter, string language) { if (value == null) return value; return value.ToString(); }
    猜你喜欢
    • 2019-01-22
    • 1970-01-01
    • 1970-01-01
    • 2012-07-20
    • 2014-12-08
    • 2015-11-25
    • 2011-06-18
    • 2016-10-07
    • 2010-10-02
    相关资源
    最近更新 更多