首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >获取DataGrid行值

获取DataGrid行值
EN

Stack Overflow用户
提问于 2016-04-24 21:08:46
回答 1查看 710关注 0票数 0

我正在开发一个wpf应用程序,我有一个名为datagrid的“买方”,并且我希望在选中复选框时访问行值

我已经读了一些关于stackoverflow的问题,但都超出了我的理解,我还不能理解它们:(

下面是我的datagrid xaml代码:

代码语言:javascript
运行
复制
<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,我过去常常通过以下代码打印值:-

代码语言:javascript
运行
复制
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
EN

Stack Overflow用户

发布于 2016-04-24 21:44:34

试试这个..。(使用http://www.kellydun.com/wpf-relaycommand-with-parameter/中的RelayCommand )

代码语言:javascript
运行
复制
public class BasePropertyChanged : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

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

ViewModel.....

代码语言:javascript
运行
复制
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

    }
}

买方类别.....

代码语言:javascript
运行
复制
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.....

代码语言:javascript
运行
复制
    <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>

别忘了在视图代码隐藏中设置你的DataContext ...

代码语言:javascript
运行
复制
this.DataContext = new Base_ViewModel();
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36823524

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档