wpf datagrid:在textbox列中写入值

时间:2013-10-07 15:35:02

标签: wpf datagrid datagridtextcolumn

我有一些包含一些列的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;

因为不是绑定......如何将其更改为绑定?

3 个答案:

答案 0 :(得分:1)

最简单的方法是创建一个数据类型类,该类具有public中每列的DataGrid属性。您应该在此类中实现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;

答案 1 :(得分: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>

答案 2 :(得分:0)

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

 this.DataGrid.Items.Refresh();

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

但是,最好按照MVVM模式规则实现INotifyPropertyChanged。