2016-04-24 153 views
0

我開發一個WPF應用程序,我有一個「買家」命名DataGrid和當複選框被選中
我已經閱讀計算器的一些問題,但都去了我的頭,我想要訪問的行值,我是無法理解他們作爲業餘尚未:(
這是我的DataGrid XAML代碼: -獲取DataGrid行值

<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秒。應該是第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 
+0

你能還包括是代碼點擊按鈕時執行? –

+0

OK等待我將它添加到您 –

+0

越來越第1列(ID)的問題,因爲ItemArray指向該列(Row.ItemArray [1])。使用ItemArray索引遍歷列。 – ErnestoDeLucia

回答

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> 

而且不要忘記設置你的DataContext在查看代碼隱藏....

this.DataContext = new Base_ViewModel(); 
+0

我沒有正確的理解這個代碼,但我會努力實現並rply回來... –

+0

我無法正確地使用此代碼,但後來我用WinForm窗口在wpf爲dat一個目的:p,thanx雖然你的回覆:) –