0

当 DataGrid 没有行时,我想在 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;}
  }

编辑:

事实证明,我的数据网格的“删除行”按钮是从实际的DataGrid 中删除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 分开的任何想法?我真的不喜欢这个修复。

4

1 回答 1

-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;

未来的工作。您可以将触发器绑定到数据网格控件。

于 2012-11-10T02:19:46.087 回答