获取DataGrid行值

时间:2016-04-24 13:08:46

标签: c# wpf xaml checkbox datagrid

我正在开发一个wpf应用程序,我有一个名为datagrid的“买家”,我希望在选中复选框时访问行值。我已经阅读了有关stackoverflow的一些问题,但所有问题都超出了我的脑海,我不是能够将它们理解为amatuer :(
这是我的datagrid 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 个答案:

答案 0 :(得分:0)

试试这个....(使用这里的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();