首页 文章

如何在wpf c#中的特定行中获取单元格的值?

提问于
浏览
0

我是wpf的新手 . 我想知道如何在特定行上获得单元格值 .

我能做到这就是winform的datagridview . 但我不知道如何在wpf的datagrid中执行此操作 . 下面是我的winform的datagridview的代码:

for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
    text = "P" + String.Format("{0:c}", Convert.ToDouble(dataGridView1.Rows[i].Cells[1].Value).ToString("0.00"));

    graphic.DrawString(text, font7Reg, new SolidBrush(System.Drawing.Color.Black), new RectangleF(x, y, width, height), drawFormatRight);
}

我将如何将此代码转换为wpf的datagrid:

dataGridView1.Rows[i].Cells[1].Value

2 回答

  • -1

    如果已将 DataGridItemsSource 属性设置或绑定到 IEnumerable<T> ,则可以将 Items 集合中的每个对象强制转换为 T ,然后访问其任何属性,例如:

    foreach(var item in salesGrid.Items.OfType<YourType>())
    {
        string text = item.YourProperty.ToString();
    }
    

    或者您可以直接遍历 ItemsSource 集合 .

  • -1

    数据绑定技术通常用于WPF应用程序 .

    步骤1创建类并在其中定义属性 . (模型的创建)

    public class Student : NotifyPropertyChanged
    {
        string _name;
        public string Name
        {
            get { return _name; }
            set { _name = value; OnPropertyChanged("Name"); }
        }
    
        string _address;
        public string Address
        {
            get { return _address; }
            set { _address = value; OnPropertyChanged("Address"); }
        }
    
        int _age;
        public int Age
        {
            get { return _age; }
            set { _age = value; OnPropertyChanged("Age"); }
        }
    }
    
    
    
    public abstract class NotifyPropertyChanged : INotifyPropertyChanged
    {
        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
    }
    

    NotifyPropertyChanged类在触发对象时将其标记为notigy

    步骤2为ViewModel创建另一个类,其中您的业务逻辑应该去创建一个模型属性列表(将在视图绑定中使用)

    public class MyContext : NotifyPropertyChanged
    {
        ObservableCollection<Student> _students = new ObservableCollection<Student>();
        public ObservableCollection<Student> Students
        {
            get { return _students; }
            set { _students = value; OnPropertyChanged("Students"); }
        }
    
        Student _selectedStudent = new Student();
        public Student SelectedStudent
        {
            get { return _selectedStudent; }
            set { _selectedStudent = value; OnPropertyChanged("SelectedStudent"); }
        }
    }
    

    STEP 3将您的逻辑绑定到视图()

    public partial class MainWindow : Window
      {
          public MainWindow()
         {
              InitializeComponent();
    
            MyContext myContext = new MyContext();
            myContext.Students = new ObservableCollection<Student>
            {
                new Student{ Name="A", Address="Address1", Age=10},
                new Student{ Name="B", Address="Address2", Age=12},
                new Student{ Name="C", Address="Address3", Age=11},
                new Student{ Name="D", Address="Address4", Age=18},
                new Student{ Name="E", Address="Address5", Age=13},
                new Student{ Name="F", Address="Address6", Age=15},
                new Student{ Name="G", Address="Address7", Age=16},
            };
    
            this.DataContext = myContext;
        }
    }
    

    STEP 4最后在你的XAML页面中添加一个DataGrid和Bind ItemsSource,如ViewModel中所定义的那样定义

    <Window x:Class="WpfApplication10.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
    
            <DataGrid ItemsSource="{Binding Students,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                      SelectedItem="{Binding SelectedStudent,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
    </Window>
    

    在上面的代码中,我展示了如何将值列表绑定到WPF DataGrid并从中选择单个值 .

相关问题