0

我有一个带有一些列的 Datagrid。它绑定到对象列表。其中一列是文本列:我需要它作为“验证”列,事实上我希望,对于每一行,这个单元格的值是“OK”或“NOT OK”,基于其他单元格中存在的值. 我不知道如何将字符串写入 Datagrid 中的某个单元格。有什么建议吗?

编辑:遵循描述 DataGrid 对象的类

public class BracketProperty
{
    [XmlAttribute]
    public int index { get; set; }
    public BracketType type { get; set;}
    public List<BracketPoint> bracketPointsList;
    [XmlIgnore]
    public String isPointSelectionEnded { get; set; }
}

EDIT2:有人告诉我这行代码不好

this.BracketsDataGrid.ItemsSource = this.currentPropertyToShow.sides[this.sideIndex - 1].brackets;

其中brackets定义为

 public ObservableCollection<BracketProperty> brackets;

因为不是绑定...我怎样才能将其更改为绑定?

4

3 回答 3

1

最简单的方法是创建一个数据类型类,该类publicDataGrid. 您应该INotifyPropertyChanged在此类中实现接口。

然后您可以拥有一个可以在PropertyChanged处理程序中更新的属性:

private void YourDataType_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
    if (Property1 == "SomeValue" && Property2 > 0) ValidationProperty = "OK";
    else ValidationProperty = "NOT OK"; // Add your own condition above of course
}

在构造函数中:

PropertyChanged += YourDataType_PropertyChanged;
于 2013-10-07T16:07:23.313 回答
0

使用价值转换器

public partial class MainWindow : Window
{
    public ObservableCollection<Employee> EmpList { get; set; }
    public MainWindow()
    {

        InitializeComponent();
        this.DataContext = this;
        EmpList = new ObservableCollection<Employee> { 
            new Employee(1, "a"), 
            new Employee(2, "b"), 
            new Employee(3, "c") };
    }
}

public class NumValueConverter : IValueConverter
{

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        int v = int.Parse(value.ToString());
        if (v < 3) 
            return "YES";
        else 
            return "NO";
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

public class Employee
{
    public int Salary { get; set; }
    public string Name { get; set; }

    public Employee(int s, string n)
    {
        Salary = s;
        Name = n;
    }
}

XAML

<Window x:Class="garbagestack.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:cv="clr-namespace:garbagestack"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.Resources>
            <cv:NumValueConverter x:Key="cv1"/>
        </Grid.Resources>
        <DataGrid AutoGenerateColumns="False" Margin="12,99,12,12" Name="dg1" ItemsSource="{Binding EmpList}" >
            <DataGrid.Columns>
                <DataGridTextColumn Header="NewValue" Binding="{Binding Salary}"/>
                <DataGridTextColumn Header="NewValue" Binding="{Binding Name}"/>
                <DataGridTextColumn Header="NewValue" Binding="{Binding Salary,Converter={StaticResource cv1}}">

                </DataGridTextColumn>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>
于 2013-10-07T16:04:50.133 回答
0

我需要自己回答问题。我还没有实现任何 INotifyPropertyChanged 接口或转换器......在设置控制变量的值后添加的唯一代码如下:

 this.DataGrid.Items.Refresh();

并且文本正确显示在网格中...

但是,最好遵循 MVVM 模式规则来实现 INotifyPropertyChanged。

于 2013-10-08T07:30:36.707 回答