C# 当属性设置为另一个mode=twoway的值时,INotifyPropertyChanged不起作用
我的Xaml 当任何用户输入少于6个字符的字符串时,文本框应显示错误消息。相反,文本框文本保持与用户输入相同。但变量值正在按预期变化C# 当属性设置为另一个mode=twoway的值时,INotifyPropertyChanged不起作用,c#,data-binding,winrt-xaml,C#,Data Binding,Winrt Xaml,我的Xaml 当任何用户输入少于6个字符的字符串时,文本框应显示错误消息。相反,文本框文本保持与用户输入相同。但变量值正在按预期变化 我正在使用WinRT应用程序,请提前帮助,谢谢。我将通过以下方式更改您的xaml代码: private string myVar; public string MyVal { get { return myVar; } set {
我正在使用WinRT应用程序,请提前帮助,谢谢。我将通过以下方式更改您的xaml代码:
private string myVar;
public string MyVal
{
get
{
return myVar;
}
set
{
if (value.Length > 6)
myVar = value;
else
myVar = "Not a valid INPUT";
OnPropertyChanged("MyVal");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
当我在WPF应用程序中使用您的示例时,它很容易工作。 请记住,当控件失去焦点时触发。 我添加了第二个文本框,所以当你改变焦点时,第一个绑定的文本框将触发事件 查看
<TextBox Text="{Binding MyVal, Mode=TwoWay, IsAsync=true}"></TextBox>
视图(XAML)
问题是您正在更改setter中的值,然后直接触发INPC事件。但是,由于当前仍在更改该值,文本框将忽略该事件。另见 在.NET 3.5/4.0/4.5中,这一具体实现已多次更改,因此它目前(4.5)的工作方式与您预期的一样(但有其他副作用,例如绑定到double时) 对于您来说,最简单的解决方案是稍微延迟触发INPC事件,这意味着文本框将被迫再次读取(可能已更新)值:
namespace WpfApplication1
{
class ViewModel : INotifyPropertyChanged
{
private string myVar;
public string MyVal
{
get
{
return myVar;
}
set
{
if (value.Length > 6)
myVar = value;
else
myVar = "Not a valid INPUT";
NotifyPropertyChanged("MyVal");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
public ViewModel() { }
}
}
向我们展示您的OnPropertyChanged实现。正在处理的事件在哪里?这不是通知用户无效输入的正确方法。如果我是用户,我永远不会使用这样的应用程序来更改我当时输入的文本。为了正确的方法@乔什。是的@SriramSakthivel先生,但我只有两个三个变量。感谢您在Winrt中提供alternative.im,因此没有选项显示
IsAsync
@user3510665抱歉,我忘记了这个事实。你测试过我的第一个解决方案吗?是的,Guerudo先生,我测试过,但没有解决方案
<TextBox Text="{Binding MyVal, Mode=TwoWay, IsAsync=true}"></TextBox>
namespace WpfApplication1
{
public partial class MainWindow : Window
{
public MainWindow()
{
this.DataContext = new ViewModel();
InitializeComponent();
}
}
}
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TextBox HorizontalAlignment="Left" Height="23" Margin="223,173,0,0" TextWrapping="Wrap" Text="{Binding MyVal, Mode=TwoWay}" VerticalAlignment="Top" Width="120"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="281,252,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
</Grid>
</Window>
namespace WpfApplication1
{
class ViewModel : INotifyPropertyChanged
{
private string myVar;
public string MyVal
{
get
{
return myVar;
}
set
{
if (value.Length > 6)
myVar = value;
else
myVar = "Not a valid INPUT";
NotifyPropertyChanged("MyVal");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
public ViewModel() { }
}
}
Dispatcher.CurrentDispatcher.BeginInvoke(NotifyPropertyChanged("MyVal"));