我对Wpf真的很陌生,我尝试了各种控制。我从Metro Mahapps演示中获取了这个ViewModel并且只留下(假设我没有错过任何东西)处理数据验证的代码(数字大于10):
public class Modelis : INotifyPropertyChanged, IDataErrorInfo
{
int? _integerGreater10Property;
public Modelis(IDialogCoordinator dialogCoordinator)
{
}
public int? IntegerGreater10Property
{
get { return this._integerGreater10Property; }
set
{
if (Equals(value, _integerGreater10Property))
{
return;
}
_integerGreater10Property = value;
RaisePropertyChanged("IntegerGreater10Property");
}
}
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Raises the PropertyChanged event if needed.
/// </summary>
/// <param name="propertyName">The name of the property that changed.</param>
protected virtual void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public string this[string columnName]
{
get
{
if (columnName == "IntegerGreater10Property" && this.IntegerGreater10Property < 10)
{
return "Number is not greater than 10";
}
return null;
}
}
public string Error { get { return string.Empty; } }
}
然后,我在我的窗口中引用它(也取自演示):
xmlns:Registracija="clr-namespace:Registracijos_sistema"
mc:Ignorable="d"
d:DesignHeight="600"
d:DesignWidth="800"
d:DataContext="{d:DesignInstance Registracija:Modelis}"
但是当我添加一个文本框(再次从演示中)通知数字是否不大于10时,它似乎不起作用:
<TextBox
Controls:TextBoxHelper.Watermark="Number smaller than 10"
Text="{Binding IntegerGreater10Property,
ValidatesOnDataErrors=True,
UpdateSourceTrigger=PropertyChanged,
NotifyOnValidationError=True}"
Margin="104,197,47,104" />
我做错了什么?
答案 0 :(得分:0)
尝试将以下内容添加到MetroWindow xaml:
.waitForConnected()
并且你的窗口后面的代码中是否有依赖属性,就像这样?
xmlns:Dialog="clr-namespace:MahApps.Metro.Controls.Dialogs;assembly=MahApps.Metro"
Dialog:DialogParticipation.Register="{Binding}"
提示:如果您使用的是Visual Studio,则可以键入propdp并点击“TAB”以自动添加代码。然后,只需输入缺失的值。
然后回到我们的MetroWindow xaml,改变这个:
public Modelis MyModelis
{
get { return (Modelis)GetValue(MyModelisProperty); }
set { SetValue(MyModelisProperty, value); }
}
// Using a DependencyProperty as the backing store for MyModelis. This enables animation, styling, binding, etc...
public static readonly DependencyProperty MyModelisProperty =
DependencyProperty.Register("MyModelis", typeof(Modelis), typeof(Window1), new PropertyMetadata(new Modelis(DialogCoordinator.Instance)));
到此:
d:DataContext="{d:DesignInstance Registracija:Modelis}"