2014-02-11 35 views
2

我試圖編寫一個代碼,用於從文本框中獲取3個值,如果某些複選框在同一行中被選中。 任何人都知道一個簡單(或困難)的方式來做到這一點?如果在數據網格中選中複選框,獲取整行值C#

我的DataGrid是這樣的:enter image description here

我有發現在文件系統的某處特定類型(XML.config)的文件,在那之後我打電話說得到了一些字符串從一個方法Load按鈕文件,找到它們的子串,並將它們放在3個分隔的列表中。這些值在DataGrid中是Type,MapTo和Name。 我通過把所有3只列出了一個的ObservableCollection做到這一點之後,我要送這ObservalableCollection到DataGrid中這樣的:

ObservableCollection<Tuple<string, string, string>> _obsCollection = new ObservableCollection<Tuple<string, string, string>>(); 
public ObservableCollection<Tuple<string, string, string>> MyObsCollection 
{ 
    get { return _obsCollection; } 
} 
tabela.ItemsSource = _obsCollection; 

這是XAML代碼顯示綁定:

<DataGrid Grid.Column="0" AutoGenerateColumns="False" Height="206" HorizontalAlignment="Left" Margin="12,265,0,0" Name="tabela" VerticalAlignment="Top" Width="556" SelectionChanged="tabela_SelectionChanged" Grid.RowSpan="2" ItemsSource="Binding MyObsCollection"> 
      <DataGrid.Columns> 

       <DataGridTextColumn Header="Type" Width="122" Binding="{Binding Item1}"/> 
       <DataGridTextColumn Header="MapTo" Width="122" Binding="{Binding Item2}"/> 
       <DataGridTextColumn Header="Name" Width="121" Binding="{Binding Item3}"/> 

       <DataGridTemplateColumn Header="Controller"> 
        <DataGridTemplateColumn.CellTemplate> 
         <DataTemplate> 
          <CheckBox IsChecked="{Binding DataGridChecked}"/> 
         </DataTemplate> 
        </DataGridTemplateColumn.CellTemplate> 
       </DataGridTemplateColumn> 

       <DataGridTemplateColumn Header="Service"> 
        <DataGridTemplateColumn.CellTemplate> 
         <DataTemplate> 
          <CheckBox IsChecked="{Binding DataGridChecked}"/> 
         </DataTemplate> 
        </DataGridTemplateColumn.CellTemplate> 
       </DataGridTemplateColumn> 

       <DataGridTemplateColumn Header="Injection"> 
        <DataGridTemplateColumn.CellTemplate> 
         <DataTemplate> 
          <CheckBox IsChecked="{Binding DataGridChecked}"/> 
         </DataTemplate> 
        </DataGridTemplateColumn.CellTemplate> 
       </DataGridTemplateColumn> 

      </DataGrid.Columns> 

     </DataGrid> 

我實際上試圖完成循環所有3列包含複選框,以查看其中哪些被選中,如果同一行中的任何3被選中,則需要將該行中的所有3個字符串值發送到某個變量。 任何人都可以幫助我。例如,我不知道如何從數據網格中的複選框獲取isSelected屬性。 我在做很多研究,所有能夠找到的都是DataGridView的示例,DataGrid幾乎沒有。

+0

'的ObservableCollection <組<字符串,字符串,字符串>>' - 你真的應該建立一個適當的強類型數據模型,而不是說怪物,請。 –

+0

@HighCore感謝評論...我仍然是一名初學者,我不知道該怎麼做......您是否知道一些關於創建數據模型的良好鏈接或教程?我知道這種代碼很混亂,但我仍然在嘗試瘋狂的事情並探索它們。 –

回答

2

而不是使用Tuple創建自己的類的,說RowData所有你想顯示爲列的屬性:

public class RowData: INotifyPropertyChanged 
{ 
    //implement INotifyPropertyChanged 

    public string Type { get; set; } 
    public string MapTo { get; set; } 
    public string Name { get; set; } 
    public bool Controller { get; set; } 
    public bool Service { get; set; } 
    public bool Injection { get; set; } 
} 

變化ObservableCollection使用你的類型

public ObservableCollection<RowData> MyObsCollection { get { .... } } 

,並設置AutoGenerateColumns="True"DataGrid

<DataGrid 
    Grid.Column="0" 
    Grid.RowSpan="2" 
    AutoGenerateColumns="True" 
    Height="206" 
    Width="556" 
    HorizontalAlignment="Left" 
    Margin="12,265,0,0" 
    Name="tabela" 
    VerticalAlignment="Top" 
    SelectionChanged="tabela_SelectionChanged" 
    ItemsSource="{Binding MyObsCollection}"/> 

,然後讓其中任何3 CheckBoxes選擇你的項目做:

var selectedList = MyObsCollection.Where(n => n.Controller || n.Service || n.Injection).ToList(); 
+0

@dkozi我做了這樣的一切,但現在我只有一行的空datagrid。我是否應該保留文本框行的所有綁定,並且應該使用Controller,Service和Injection屬性綁定複選框行? –

+1

沒有,綁定當然是錯的,因爲現在我有了新的名字,Item1,Item2和Item3不能再用於綁定了。非常感謝你的幫助。 –

+0

沒問題。很高興幫助 – dkozl

相關問題