【问题标题】:Get DataGrid row values获取 DataGrid 行值
【发布时间】:2016-08-17 20:06:20
【问题描述】:

我正在开发一个 wpf 应用程序,并且我有一个名为 datagrid 的“买家”,我希望在选中复选框时访问行值
我已经阅读了一些关于 stackoverflow 的问题,但都超出了我的想象,我无法将它们理解为 amatuer :(
这是我的数据网格 xaml 代码:-

<DataGrid x:Name="buyer" SelectionMode="Single" HorizontalAlignment="Left" SelectionUnit="FullRow" VerticalAlignment="Top" Height="550" Width="992" HorizontalScrollBarVisibility="Visible" IsReadOnly="True" AutoGenerateColumns="False" FrozenColumnCount="1" Margin="0,45,0,0" SelectionChanged="RowFocus" TargetUpdated="buyer_TargetUpdated">
    <DataGrid.Columns>
        <DataGridTemplateColumn Header="Joining" >
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <CheckBox IsChecked="{Binding IsSelected,UpdateSourceTrigger=PropertyChanged}"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
         </DataGridTemplateColumn>
         <DataGridTextColumn Header="ID" Binding="{Binding buy_id}"/>
         <DataGridTextColumn Header="Name" Binding="{Binding bname}"/>
         <DataGridTextColumn Header="Number" Binding="{Binding mobileno}"/>
    </DataGrid.Columns>
</DataGrid>

我在同一个窗口上有一个按钮,单击该按钮应该会从选中 CheckBox 的行中为我提供值

编辑:目前,我正在通过在控制台中写入来检查 CheckBox 是否正在工作。 CheckBox 也应该是第 0 列,对吧?但是当我在控制台中打印它时,我得到了下一列的值,即 ID,我曾经通过输入以下代码来打印该值:-

private void Button_Click_3(object sender, RoutedEventArgs e)
    {
        /*  int i = 0;
          Console.WriteLine("hey");

          foreach (var item in buyer.Items)
          {

              string s = (buyer.Items[i] as DataRowView).Row.ItemArray[0].ToString();
              if (i==0)
              {
                  Console.WriteLine(s);
                  var row = buyer.ItemContainerGenerator.ContainerFromItem(item) as DataGridRow;


              }
              i++;
          }*/
        if (buyer.SelectedItems.Count > 0)
            {
                for (int i = 0; i < buyer.SelectedItems.Count; i++)
                {

                    System.Data.DataRowView selectedFile =       (System.Data.DataRowView)buyer.SelectedItems[i];
                    string str =       Convert.ToString(selectedFile.Row.ItemArray[0]);
  Console.WriteLine(str);
                }
            }
        }

 I used both commented and uncommented code

【问题讨论】:

  • 能否也包含点击按钮时执行的代码?
  • 好的,等我把它添加到问题中
  • 您将获得第 1 列 (ID),因为 ItemArray 指向该列 (Row.ItemArray[1])。使用 ItemArray 索引遍历列。
  • @ErnestoDeLucia 但我在单元格数组中放了 0,对吗?那是 dat 发生的,另见我编辑的问题..
  • 当你遍历 items 数组时,你会得到多少列?除了 ID 之外,其他列的值是您期望的吗?

标签: c# wpf xaml checkbox datagrid


【解决方案1】:

试试这个....(使用此处找到的 RelayCommand http://www.kellydun.com/wpf-relaycommand-with-parameter/)

public class BasePropertyChanged : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
}

视图模型.....

class Base_ViewModel : BasePropertyChanged
{

    public RelayCommand<ObservableCollection<buyer>> ButtonClickCommand { get; set; }

    private ObservableCollection<buyer> _buyer;
    public ObservableCollection<buyer> buyer
    {
        get { return _buyer; }
        set { _buyer = value; }
    }


    public Base_ViewModel()
    {
        ButtonClickCommand = new RelayCommand<ObservableCollection<buyer>>(OnButtonClickCommand);
        buyer = new ObservableCollection<ViewModels.buyer>();
        buyer.Add(new buyer() { buy_id = 1, bname = "John Doe", mobileno = "" });
        buyer.Add(new buyer() { buy_id = 1, bname = "Jane Doe", mobileno = "" });
        buyer.Add(new buyer() { buy_id = 1, bname = "Fred Doe", mobileno = "" });
        buyer.Add(new buyer() { buy_id = 1, bname = "Sam Doe", mobileno = "" });

    }

    private void OnButtonClickCommand(ObservableCollection<buyer> obj)
    {  // put a break-point here and obj will be the List of Buyer that you can then step though

    }
}

买家类.....

public class buyer : BasePropertyChanged
{
    private bool _IsSelected;

    public bool IsSelected
    {
        get { return _IsSelected; }
        set { _IsSelected = value; }
    }

    private string _bname;

    public string bname
    {
        get { return _bname; }
        set { _bname = value; NotifyPropertyChanged("bname"); }
    }

    private int _buy_id;

    public int buy_id
    {
        get { return _buy_id; }
        set { _buy_id = value; NotifyPropertyChanged("buy_id"); }
    }

    private string _mobileno;

    public string mobileno
    {
        get { return _mobileno; }
        set { _mobileno = value; NotifyPropertyChanged("mobileno"); }
    }
}

XAML .....

    <StackPanel>
        <DataGrid x:Name="buyer" ItemsSource="{Binding buyer}" SelectionMode="Single" HorizontalAlignment="Left" SelectionUnit="FullRow" IsReadOnly="True" AutoGenerateColumns="False" FrozenColumnCount="1" >
            <DataGrid.Columns>
                <DataGridTemplateColumn Header="Joining" >
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox IsChecked="{Binding IsSelected,UpdateSourceTrigger=PropertyChanged}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTextColumn Header="ID" Binding="{Binding buy_id}"/>
                <DataGridTextColumn Header="Name" Binding="{Binding bname}"/>
                <DataGridTextColumn Header="Number" Binding="{Binding mobileno}"/>
            </DataGrid.Columns>
        </DataGrid>
        <Button Content="Button" Command="{Binding ButtonClickCommand}" CommandParameter="{Binding ElementName=buyer, Path=ItemsSource}" Margin="0,202,0,0"></Button>
    </StackPanel>

别忘了在 View 代码隐藏中设置您的 DataContext....

this.DataContext = new Base_ViewModel();

【讨论】:

  • 我没有正确理解这段代码,但我会尝试实现并会回复...
  • 我无法正确使用此代码,但后来我将 wpf 中的 winform 窗口用于 dat 一个目的 :p,thanx 尽管您的回复 :)
猜你喜欢
  • 1970-01-01
  • 2011-07-04
  • 1970-01-01
  • 2019-12-21
  • 2012-11-11
  • 2016-10-13
  • 1970-01-01
  • 1970-01-01
  • 2021-12-26
相关资源
最近更新 更多