无法分配一个NULL值到一个空Int32?通过绑定 [英] Cannot Assign a Null Value to a Nullable Int32? via Binding

查看:127
本文介绍了无法分配一个NULL值到一个空Int32?通过绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不能分配通过文本框绑定到Int32一个空值? 如果文本框为空,则Int32Null设置不叫。
一个红色的边框围绕TexBox指示验证异常。

这只是没有任何意义,因为的Int32?可为空。如果用户从文本框的整数值,我要叫这样的属性分配给空的设置。

当它开始int32Null = null,且文本框不红。

我想实现验证,并设置验证= true,如果文本框为空。但设置仍然没有打来电话,文本框是红色的,表示验证错误。

好像我应该可以通过绑定分配一个空值一空。

 <窗​​口x:类=AssignNull.MainWindow
            的xmlns =htt​​p://schemas.microsoft.com/winfx/2006/xaml/$p$psentation
            的xmlns:X =htt​​p://schemas.microsoft.com/winfx/2006/xaml
            的DataContext ={绑定的RelativeSource = {的RelativeSource自我}}
            标题=主窗口高度=350宽度=525>
        <电网>
            < Grid.RowDefinitions>
                < RowDefinition高度=自动/>
                < RowDefinition高度=自动/>
            < /Grid.RowDefinitions>
            < Grid.ColumnDefinitions>
                &所述; ColumnDefinition宽度=*/>
            < /Grid.ColumnDefinitions>
            <文本框Grid.Row =0Grid.Column =0文本={绑定路径= Int32Null,模式=双向,UpdateSourceTrigger =引发LostFocus}/>
            <文本框Grid.Row =2Grid.Column =0文本={绑定路径= StringNull,模式=双向,UpdateSourceTrigger =引发LostFocus}/>
        < /网格>
    < /窗>
 

公共部分类主窗口:窗口     {         私人的Int32? int32Null = NULL;         私人字符串stringNull =stringNull;         公共主窗口()         {             的InitializeComponent();         }         公众的Int32? Int32Null         {             {返回int32Null; }             集合{int32Null =价值; }         }         公共字符串StringNull         {             {返回stringNull; }             集合{stringNull =价值; }         }     }

设置StringNull不会被调用,并传递值不为空,而是的String.Empty。

由于设置不叫上Int32Null我不知道是什么获得通过。

也有人传递的String.Empty来的Int32?只好转换成空空字符串。

  [ValueConversion(typeof运算(的Int32?),typeof运算(字符串))
    公共类Int32nullConverter:的IValueConverter
    {
        公共对象转换(对象的值,类型TARGETTYPE,对象参数,CultureInfo的文化)
        {
            INT32? int32null =(Int32)已值;
            返回int32null.ToString();
        }

        公共对象ConvertBack(对象的值,类型TARGETTYPE,对象参数,CultureInfo的文化)
        {
            字符串将strValue =值串;
            如果(string.IsNullOrEmpty(strValue.Trim()))返回NULL;
            INT32 INT32;
            如果(Int32.TryParse(strValue中,出INT32))
            {
                返回INT32;
            }
            返回DependencyProperty.UnsetValue;
        }
    }
 

解决方案

您做出如何的类型转换器应该处理这个问题。所以,如果他们不这样做你想做的,就是把一个空字符串为你要么必须编写自己的或使用的 Binding.Converter 执行转换为你。

Cannot assign a null value via a TextBox Binding to Int32?. If the TextBox is empty then Int32Null Set is not called.
A red border is around the TexBox indicating a validation exception.

This just does not make sense as Int32? is nullable. If the user removes the integer value from the TextBox I want the Set called so the property is assigned to null.

When it starts int32Null = null and the TextBox is not red.

I tried implementing Validation and set validation = true if the TextBox is empty. But Set is still not called and TextBox is red indicating a validation error.

It seems like I should be able to assign a null value to a nullable via binding.

    <Window x:Class="AssignNull.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
            DataContext="{Binding RelativeSource={RelativeSource self}}"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <TextBox Grid.Row="0" Grid.Column="0" Text="{Binding Path=Int32Null, Mode=TwoWay, UpdateSourceTrigger=LostFocus}" />
            <TextBox Grid.Row="2" Grid.Column="0" Text="{Binding Path=StringNull, Mode=TwoWay, UpdateSourceTrigger=LostFocus}" />
        </Grid>
    </Window>

    public partial class MainWindow : Window
    {
        private Int32? int32Null = null;
        private string stringNull = "stringNull";
        public MainWindow()
        {
            InitializeComponent();
        }
        public Int32? Int32Null
        {
            get { return int32Null; }
            set { int32Null = value; }
        }
        public string StringNull
        {
            get { return stringNull; }
            set { stringNull = value; }
        }
    } 

Set StringNull does get called and value passed is not null but rather string.empty.

Since Set is not called on Int32Null I don't know what is getting passed.

It was also passing a string.empty to Int32?. Had to convert an empty string to null.

    [ValueConversion(typeof(Int32?), typeof(String))]
    public class Int32nullConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            Int32? int32null = (Int32?)value;
            return int32null.ToString();
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string strValue = value as string;
            if(string.IsNullOrEmpty(strValue.Trim())) return null;
            Int32 int32;
            if (Int32.TryParse(strValue, out int32))
            {
                return int32;
            }
            return DependencyProperty.UnsetValue;
        }
    }

解决方案

You make a false assumption on how the type converters ought to handle this. So if they do not do what you want, namely turn an empty string into null you'll either have to write your own or use a Binding.Converter that does the conversion for you.

这篇关于无法分配一个NULL值到一个空Int32?通过绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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