Reputation: 4781
I'm trying to write a code for getting 3 values from textboxes if some of checkboxes in same row is checked. Anyone know an easy(or hard) way to do this?
My datagrid looks like this:
I have Load button that finds a file of specific type(XML.config) somewhere in file system, after that I'm calling a method that gets some strings from that file, find substrings of them and put them in 3 separated lists. Those values are in datagrid as Type, MapTo and Name. I accomplish this by putting all 3 lists in one ObservableCollection and after that I'm sending that ObservalableCollection to datagrid like this:
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;
This is XAML code that shows binding:
<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>
What I'm practically trying to is to accomplish looping thou all 3 columns containing checkboxes to see which of them are selected and if any of 3 in same row is selected then I need to send all 3 string values from that row to some variable. Anyone can help me with this. For instance I don't know how to get isSelected property from checkbox in data grid. I was doing a lot of researching and all that I was able to find was examples for DataGridView, and almost nothing for DataGrid.
Upvotes: 2
Views: 4573
Reputation: 813
To get the Checked item row from DataGrid and add it into list
List<string>chzone=new List<string>();//Declare it as globally,
//Note: if DataGrid is Binded as ,Datagrid.Itemsource=dt.defaultview;
//If datagrid contain the Checkbox, get the checked item row and add it into list
private void DataGridcheckbox_Checked(object sender, RoutedEventArgs e)
{
var checker = sender as CheckBox;
if (checker.IsChecked == true)
{
var item = checker.DataContext as DataRowView;
object[] obj = item.Row.ItemArray;//getting entire row
chzone.Add(obj[0].ToString());//getting row of first column value
}
else //This is for Unchecked Scenario
{
var item = checker.DataContext as DataRowView;
object[] obj = item.Row.ItemArray;
bool res = chzone.Contains(obj[0].ToString());
if (res == true)
{
chzone.Remove(obj[0].ToString());//this is used to remove item in list
}
}
}
Upvotes: 0
Reputation: 33364
Instead of using Tuple
create your own class, say RowData
with all the properties you want to show as columns:
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; }
}
change ObservableCollection
to use your type
public ObservableCollection<RowData> MyObsCollection { get { .... } }
and set AutoGenerateColumns="True"
on 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}"/>
and then to get items where any of 3 CheckBoxes
is selected you do:
var selectedList = MyObsCollection.Where(n => n.Controller || n.Service || n.Injection).ToList();
Upvotes: 2