- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在按下某个键时验证文本框中的文本。这是显示我正在尝试做的事情的最短代码示例:
<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 FontSize="15" HorizontalAlignment="Left" Name="txtEmail" VerticalAlignment="Top" Width="135"
Text="{Binding ValidationRules.EmailAddress, ValidatesOnExceptions=True, UpdateSourceTrigger=PropertyChanged}"/>
</Grid>
</Window>
“验证规则”类:
class ValidationRules
{
string email = "";
public string EmailAddress
{
get
{
return email;
}
set
{
Console.WriteLine("Setting!");
//Only check if there is any text for now...
if (String.IsNullOrWhiteSpace(value))
{
throw new Exception();
}
email = value;
}
}
}
当我开始在文本框中键入内容时,即使我正在使用 UpdateSourceTrigger=PropertyChanged
,我也没有将“Setting”作为控制台输出。我已经完成了研究,但我能找到的所有示例都很冗长且令人困惑。如果您能指出我在验证中遇到的任何其他错误,我也将不胜感激,但如果可能,请尝试用简单的术语进行解释,因为我是 WPF 的新手。
最佳答案
这一定是您在何处设置 DataContext 的问题。
这个例子似乎工作正常:
代码:
public partial class MainWindow : Window, INotifyPropertyChanged
{
public MainWindow()
{
InitializeComponent();
ValidationRules = new ValidationRules();
}
private ValidationRules _validation;
public ValidationRules ValidationRules
{
get { return _validation; }
set { _validation = value; NotifyPropertyChanged("ValidationRules"); }
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
}
public class ValidationRules : INotifyPropertyChanged
{
string email = "";
public string EmailAddress
{
get
{
return email;
}
set
{
Console.WriteLine("Setting!");
//Only check if there is any text for now...
if (String.IsNullOrWhiteSpace(value))
{
throw new Exception();
}
email = value;
NotifyPropertyChanged("EmailAddress");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
}
XAML
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="WpfApplication1.MainWindow"
Title="MainWindow" Height="125.078" Width="236.441" x:Name="UI" >
<Grid DataContext="{Binding ElementName=UI}">
<TextBox FontSize="15" HorizontalAlignment="Left" Name="txtEmail" VerticalAlignment="Top" Width="135"
Text="{Binding ValidationRules.EmailAddress, ValidatesOnExceptions=True, UpdateSourceTrigger=PropertyChanged}"/>
</Grid>
</Window>
关于c# - UpdateSourceTrigger 不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14823119/
只是一个简短的问题: 在 wpf 中,如何在代码隐藏中设置和获取文本框的 updatesourcetrigger ? 谢谢 更新: 我遵循 AngleWPF 的代码: var bndE
我正在创建一个带有多个文本框的 WPF 窗口,当用户按下确定按钮时,我希望所有文本框都被评估为非空白。 我知道我必须使用带有'Explicit'的'UpdateSourceTrigger'的TextB
我正在尝试在按下某个键时验证文本框中的文本。这是显示我正在尝试做的事情的最短代码示例: “验证规则”类: class ValidationRules
我最近在 stack overflow 的帮助下解决了一个样式问题...(您可以在我的历史记录中看到问题/答案) 因此,我想到了以下应用于特定文本框的样式。
我正在寻找可以与我的绑定(bind)一起使用的 UpdateSourceTrigger 的自定义版本。我不知道这是否可能,或者相反,我只需要创建自己的绑定(bind)类。我正在寻找的是,而不是 Los
我有一个 TextBlock,与一个对象绑定(bind),当我更新对象的属性时,它没有反射(reflect)在 UI 上,为什么? 代码: 在 Windows1.xaml 中 和 Windows.x
xaml View 模型 public ProductVM() {
我有一个 WPF TextBox,它绑定(bind)到一个对象的(非依赖)属性(实现 INotifyPropertyChanged 以支持绑定(bind))。 验证是通过在绑定(bind)对象上实现
我有一个简单的 Converter,它向 TextBox 中输入的正数添加一个“+”符号。输入数字后,我想启动一些操作,但我不想等到 TextBox 失去焦点:我想在用户输入文本时立即更新绑定(bin
我一直在探索 Caliburn Micro MVVM 框架只是为了感受一下,但我遇到了一些问题。我有一个 TextBox 绑定(bind)到我的 ViewModel 上的字符串属性,我希望在 Text
我有以下 XAML - Start 在 ViewModel 中 - public string this[string columnName] { get {
我正在使用 ValidationRule在 TextBox在我看来,ValidationRule工作正常。 但是,当我处于 Command 的 canExecute 方法的状态时,就会出现问题。 (在
我有以下代码: public partial class Window1 : Window { public Window1() { Initi
MSDN 文档指出: Bindings that are TwoWay or OneWayToSource listen for changes in the target property and
我在 Windows 窗体中托管 WPF 用户控件 在 wpf 用户控件中,我使用来自 wpfToolkit.extended 的时间选择器 如果我使用向上键或向下键,或者只是在文本字段中输入时间,则
我正在使用 Xamarin.Forms,我的 View 完全在 XAML 中创建。我也在使用 Xamarin.Forms.Labs。 我已将 EntryCell 绑定(bind)到这样的属性: 该页
“名称”是在运行时与 TextBlock 绑定(bind)的对象的属性。我已经实现了 INotifyPropertyChanged,所以代码工作正常。我的问题是:如果我将 PropertyChang
我正在努力使用 DataGrid 捕获事件。我想要实现的是,当用户在数据网格单元格的复选框上单击一次时,会触发一个事件,我可以获得当前单元格值。但是,CellChangedEvent 仅在选择更改时触
我正在学习 WPF 教程系列。目前我在这一步 http://www.wpf-tutorial.com/data-binding/responding-to-changes/ 我遇到的问题是,我希望文本
我有两个文本框,如果我更改其中一个中的值,则应计算另一个文本框中的值。这需要双向发挥作用。 在我的 ViewModel 中,我有一个名为 NewProductCount 的属性/对象 priv
我是一名优秀的程序员,十分优秀!