我想在没有行的情况下在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中删除)...并且由于某种原因这会阻止验证被调用的电话。
再次查看:
<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.
}
错误边框&amp;图标不会显示在数据网格周围。
但是如果我把一个直接改变了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分开的任何想法?我真的不喜欢这个修复。
答案 0 :(得分:-1)
尝试将数据网格放入边框并为其附加触发器,以便在数据网格为空时将其设为红色
<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;
未来的工作。您可以将触发器绑定到数据网格控件。