WPF绑定功能不能使用INT类型的属性正常工作 [英] WPF binding not working properly with properties of int type

查看:1538
本文介绍了WPF绑定功能不能使用INT类型的属性正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 INT 的属性类型中绑定到文本框我的视图模型。一切工作正常,双向结合优秀作品除了一种情况 -



如果我明确的价值文本框,属性setter不会被调用,虽然值在文本框清零,房地产仍占据前值。



有没有人遇到了类似的问题? ?有没有这方面的任何解决办法



下面是属性 -

 公众诠释MaxOccurrences 
{
得到
{
返回this.maxOccurrences;
}

{
如果(this.maxOccurrences =价值!)
{
this.maxOccurrences =价值;
base.RaisePropertyChanged(MaxOccurrences);
}
}
}

下面是我怎么绑定在XAML属性 -

 <文本框的文本={绑定路径= MaxOccurrences,模式=双向,
NotifyOnSourceUpdated = TRUE,UpdateSourceTrigger =的PropertyChanged}
的Horizo​​ntalAlignment =中心宽度=30保证金=0,0,5,0/>


解决方案

我有类似的问题。



您只需要更新代码为:

 <文本框的文本={绑定路径= MaxOccurrences,模式=双向,TargetNullValue = {X:静态SYS:}的String.Empty,
NotifyOnSourceUpdated = TRUE,UpdateSourceTrigger =的PropertyChanged}
的Horizo​​ntalAlignment =中心宽度=30保证金=0,0,5,0/>


I am having a property of int type in my view model which is bound to a TextBox. Everything works properly, TwoWay binding works fine except in one case -

If I clear the value of TextBox, property setter doesn't gets called and although value is cleared in TextBox, property still holds the previous value.

has anyone faced similar issue? is there any workaround for this?

Here is the property -

public int MaxOccurrences
{
    get
    {
        return this.maxOccurrences;
    }
    set
    {
        if (this.maxOccurrences != value)
        {
            this.maxOccurrences = value;
            base.RaisePropertyChanged("MaxOccurrences");
        }
    }
}

Here is how I am binding the property in xaml -

<TextBox Text="{Binding Path=MaxOccurrences, Mode=TwoWay, 
    NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}" 
    HorizontalAlignment="Center" Width="30" Margin="0,0,5,0"/>

解决方案

I had the similar problem.

You just need to update the code as:

<TextBox Text="{Binding Path=MaxOccurrences, Mode=TwoWay, TargetNullValue={x:Static sys:String.Empty},
NotifyOnSourceUpdated=True,  UpdateSourceTrigger=PropertyChanged}"  
HorizontalAlignment="Center" Width="30" Margin="0,0,5,0"/> 

这篇关于WPF绑定功能不能使用INT类型的属性正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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