[英]WPF Datagrid ValidationRules on ItemsSource
我想在沒有行的DataGrid周圍放置紅色邊框(我正在綁定到ItemsSource)。
因此,我遵循此指南進行WPF驗證:
http://www.codeproject.com/Articles/15239/Validation-in-Windows-Presentation-Foundation
無論如何,當Text = ""
時,使文本框具有這樣的錯誤非常簡單:
甚至自定義錯誤:
我嘗試調試,並且從未調用綁定在ItemsSource中的ValidationRules。
<DataGrid ...>
<DataGrid.ItemsSource>
<Binding Path="Lines" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<DataGridValidationRule
MiniumRows="1"
MaximumRows="100"
ErrorMessage="must have between 1 and 100 rows">
</DataGridValidationRule>
</Binding.ValidationRules>
</Binding>
</DataGrid.ItemsSource>
</DataGrid>
然后,DataGridValidtionRule類如下所示:
public class public class StringRangeValidationRule : ValidationRule
{
private int _minimumRows = -1;
private int _maximumRows = -1;
private string _errorMessage;
public int MinimumRows
{
get { return _minimumRows ; }
set { _minimumRows = value; }
}
public int MaximumRows
{
get { return _maximumLength; }
set { _maximumLength = value; }
}
public string ErrorMessage
{
get { return _errorMessage; }
set { _errorMessage = value; }
}
public override ValidationResult Validate(object value,
CultureInfo cultureInfo)
{
ValidationResult result = new ValidationResult(true, null);
ObservableCollection<Lines> lines = (ObservableCollection<Lines>) value;
if (lines.Count < this.MinimumRows||
(this.MaximumRows> 0 &&
lines.Count > this.MaximumRows))
{
result = new ValidationResult(false, this.ErrorMessage);
}
return result;
}
和“線”類
public class Line
{
public Line(string f, string b)
{
foo = f;
bar = b;
}
public string Foo {get; set;}
public string Bar {get; set;}
}
編輯:
事實證明,我的數據網格的“刪除行”按鈕是從ObservableCollection
中刪除的,而不是從實際的DataGrid中刪除的(它是在ViewModel上刪除的)……並且由於某種原因,這阻止了Validation調用的調用。
再次查看:
<DataGrid Name="mygrid">
<DataGrid.ItemsSource>
<Binding Path="Lines" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<DataGridValidationRule
MiniumRows="1"
MaximumRows="100"
ErrorMessage="must have between 1 and 100 rows">
</DataGridValidationRule>
</Binding.ValidationRules>
</Binding>
</DataGrid.ItemsSource>
</DataGrid>
所以如果我在ViewModel中:
void delete(Line l)
{
Lines.Remove(l); //if you delete everything (grid empty) there won't be any error shown.
}
錯誤邊框和圖標不會在數據網格周圍顯示。
但是,如果我改為放置一個直接更改ItemsSource的事件,如下所示:
void delete(Line l)
{
Lines.Remove(l);
myView.mygrid.ItemsSource = Lines; // this magically fixes everything... even though it was already bound to Lines... though i hate to directly access the View from within the ViewModel.
}
我不知道為什么會這樣...但是已經解決了。 關於如何將視圖與VM分開的任何想法? 我不太喜歡此修復程序。
嘗試將datagrid置於邊框中,並在其上附加觸發器以使其在datagrid為空后變為紅色
<Border Margin="20" >
<Border.Style>
<Style TargetType="Border">
<Style.Triggers>
<DataTrigger Binding="{Binding ISEmpty}" Value="True">
<Setter Property="BorderThickness" Value="2" />
<Setter Property="BorderBrush" Value="Red" />
</DataTrigger>
</Style.Triggers>
</Style>
</Border.Style>
<DataGrid Name="myGrid" AutoGenerateColumns="True" ItemsSource="{Binding Path=Lecturers}" >
</DataGrid>
</Border>
清除網格時,將IsEmpty屬性設置為true
private bool iSEmpty;
public bool ISEmpty
{
get { return iSEmpty; }
set
{
iSEmpty = value;
NotifyPropertyChanged("ISEmpty");
}
}
清除您的商品來源集合
viewmodel.Lecturers.Clear();
this.viewmodel.ISEmpty = true;
未來的工作。 您可以將觸發器綁定到數據網格控件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.