Get DataGrid row values - c#

I am developing a wpf application and i have a "buyer" named datagrid and i want access row values when a checkbox is checkedI have read some questions on stackoverflow but all went over my head, i was not able to understand them as amatuer yet :( Here is my datagrid xaml code:-
<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>
I have a button on the same window, which on clicking should give me values from the rows where the CheckBox is checked
Edit: Currently, I am checking if the CheckBox is working by writing in console. Also the CheckBox should be the 0th column, right? But when I print it in the console I get the value of the next column i.e. ID, I used to print the value by putting the following code :-
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

Try this.... (using RelayCommand found here 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));
}
}
}
ViewModel.....
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
}
}
Buyer Class.....
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>
And don't forget to set your DataContext in the View code-behind....
this.DataContext = new Base_ViewModel();

Related

GridView ComboBox - Bind selected element property

EDIT : Working better, but still not perfect :
SelectedFace has been set as ComboElement (still couldn't manage to do the job without this additional parameter).
Still not working : When I create a new Tool, SelectedFace is updated correctly, but if I want to edit an existing one, no Update is made(no binding).
End Edit
I have a GridView, in which there is a ComboBox to select on which face each tool located.
I cannot manage to bind the selected value to my object property.
Here are the properties as declared in my Tool object :
private long face = 0;
public long Face
{
get { return face; }
set
{
this.face = value;
this.NotifyPropertyChanged("Face");
}
}
private ComboElement selectedFace;
public ComboElement SelectedFace
{
get { return selectedFace; }
set
{
this.selectedFace = value;
this.Face = value.MyInt;
this.NotifyPropertyChanged("SelectedFace");
}
}
private ObservableCollection<ComboElement> comboFaceSlot=new ObservableCollection<ComboElement> { new ComboElement { MyInt = 0, MyString = "Web" }, new ComboElement { MyInt = 1, MyString = "Top" }, new ComboElement { MyInt = 2, MyString = "Bottom" }, new ComboElement { MyInt = 3, MyString = "Back" } };
public ObservableCollection<ComboElement> ComboFaceSlot
{
get { return comboFaceSlot; }
set
{
this.comboFaceSlot = value;
this.NotifyPropertyChanged("ComboFaceSlot");
}
}
Nota : At the beginning I had not the SelectedFace property, I added it thinking this would solve my problem, but it didn't.
And XAML is as following :
<DataGrid Grid.Row="1" Margin="5" BorderBrush="{StaticResource WindowBorderColor}" BorderThickness="3" Width="Auto" ItemsSource="{Binding Path=ListTools}" AutoGenerateColumns="False" CanUserAddRows="True" CanUserDeleteRows="True" HorizontalAlignment="Center">
<DataGrid.Columns>
<DataGridTextColumn Header="{x:Static p:Resources.Length}" Binding="{Binding Path=Longueur}" Width="100" />
<DataGridTextColumn Header="{x:Static p:Resources.Diameter}" Binding="{Binding Path=Diameter}" Width="100" />
<DataGridTemplateColumn Header="{x:Static p:Resources.Face}">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding ComboFaceSlot}" DisplayMemberPath="MyString" SelectedItem="{Binding SelectedFace}" Width="100" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="{x:Static p:Resources.Angle}" Binding="{Binding Path=Angle}" Width="100" />
</DataGrid.Columns>
</DataGrid>
Also forgot, my ComboElement I use for all Comboboxes of my project :
public class ComboElement
{
private long myInt=-1;
public long MyInt
{
get { return myInt; }
set { myInt = value; }
}
private string myString="";
public string MyString
{
get { return myString; }
set { myString = value; }
}
public ComboElement()
{
}
public ComboElement(long id, string name)
{
this.myInt = id;
this.MyString = name;
}
}
Edit : My ViewModel
private ObservableCollection<Tool> listTool = new ObservableCollection<Tool>();
public ObservableCollection<Tool> ListTool
{
get { return listTool; }
set
{
this.listTool = value;
NotifyPropertyChanged("ListTool");
}
}
private Machine cnc = new Machine();
public Machine CNC
{
get { return cnc; }
set { this.cnc = value;NotifyPropertyChanged("CNC"); }
}
public TableTools(Machine cnc)
{
InitializeComponent();
this.CNC = cnc;
foreach(Tool slot in CNC.ListTool)
{
this.ListTool.Add(slot);
}
DataContext = this;
}
It lists a list of Tools, and I want to say on which face each element is located.
But for now something is going wrong, I tried to use several properties of my ComboBox as SelectedItem, SelectedValue, SelectedValuePath, but until now didn't manage to solve it.
Ideally, I would like to delete "SelectedFace" element, and use only "Face" property.
You need to specify the Tool class like such in your question.
The root cause is not specifying the correct SelectedValuePath of your ComboElement object to be mapped to your Tool object's Face property as the SelectedValue.
Tool Class
public class Tool
{
private ObservableCollection<ComboElement> comboFaceSlot = new ObservableCollection<ComboElement> { new ComboElement { MyInt = 0, MyString = "Web" }, new ComboElement { MyInt = 1, MyString = "Top" }, new ComboElement { MyInt = 2, MyString = "Bottom" }, new ComboElement { MyInt = 3, MyString = "Back" } };
public ObservableCollection<ComboElement> ComboFaceSlot
{
get { return comboFaceSlot; }
set
{
this.comboFaceSlot = value;
this.NotifyPropertyChanged("ComboFaceSlot");
}
}
private long face = 0;
public long Face
{
get { return face; }
set
{
this.face = value;
this.NotifyPropertyChanged("Face");
}
}
}
ToolWindow View
<DataGrid Grid.Row="1" Margin="5" BorderBrush="{StaticResource WindowBorderColor}" BorderThickness="3" Width="Auto" ItemsSource="{Binding Path=ListTools}" AutoGenerateColumns="False" CanUserAddRows="True" CanUserDeleteRows="True" HorizontalAlignment="Center">
<DataGrid.Columns>
<DataGridTextColumn Header="{x:Static p:Resources.Length}" Binding="{Binding Path=Longueur}" Width="100" />
<DataGridTextColumn Header="{x:Static p:Resources.Diameter}" Binding="{Binding Path=Diameter}" Width="100" />
<DataGridTemplateColumn Header="{x:Static p:Resources.Face}">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding ComboFaceSlot}"
DisplayMemberPath="MyString"
SelectedValuePath="MyInt"
SelectedValue="{Binding Face, UpdateSourceTrigger=PropertyChanged}" Width="100" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="{x:Static p:Resources.Angle}" Binding="{Binding Path=Angle}" Width="100" />
</DataGrid.Columns>

How to Display and select items in a Datagrid ComboBox with WPF C#, using MVVM

I want to be able to choose either "true" or "false"(boolean) from a ComboBox that is within a wpf Datagrid and be able to save that choice to my database.
I want to be able to indicate inside of a column if the row is "Active" or not through a boolean variable saved to my database as a bit(1 = true; 0 = false).
Here is my create table statement:
Create Table Statement(AccountType)
I populate the datagrid using an ObservableCollection as follows:
protected override void Get()
{
using (var dbContext = new IEMASEntitiesDataContext())
{
var accountType = from s in dbContext.AccountTypes select s;
var observable = new ObservableCollection<AccountType>(accountType);
Collection = observable;
}
}
My XAML code is as follows:
<DockPanel DataContext="{StaticResource ResourceKey=AccountTypeViewModel}" LastChildFill="True">
<ToolBar DockPanel.Dock="Top">
<Button Content="Display" Command="{Binding GetCommand}" Width="78"/>
<Button Content="Save" Command="{Binding SaveCommand}" Width="78"/>
</ToolBar>
<Grid>
<GroupBox x:Name="AccountTypeGroupBox">
<DataGrid x:Name="DataGridAccountType" ItemsSource="{Binding Collection}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="AccountType" Width="150" Binding="{Binding Path=AccountTypeName, Mode=TwoWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}"/>
<DataGridComboBoxColumn Header="Active" Width="100" SelectedValueBinding="{Binding StatusList, UpdateSourceTrigger=PropertyChanged}" SelectedValuePath="AccountTypeId" DisplayMemberPath="Active"/>
<DataGridTemplateColumn Width="50" Header="Delete">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button x:Name="btnDelete" Content="Delete" Command="{Binding DeleteCommand}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</GroupBox>
</Grid>
</DockPanel>
It doesn't work. Nothing displays within the comboboxes, and I don't know how to save the selected item to the SQL Server database when it does display. I'd appreciate some help, please. I am using LINQ TO SQL. I am a newbie :-(.
As I can understand the problem is how to bind some XAML resource as a combo ItemsSource and in addtion how to binnd the selected value of a combo to the model behind the DataGrid row.
1. List item:
<Window x:Class="SoDataGridProjectsHelpAttempt.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:soDataGridProjectsHelpAttempt="clr-namespace:SoDataGridProjectsHelpAttempt"
xmlns:system="clr-namespace:System;assembly=mscorlib"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<x:Array x:Key="CountriesArray" Type="soDataGridProjectsHelpAttempt:Country">
<soDataGridProjectsHelpAttempt:Country CountryName="Germany" CountryPop="150k"/>
<soDataGridProjectsHelpAttempt:Country CountryName="France" CountryPop="125k"/>
<soDataGridProjectsHelpAttempt:Country CountryName="Belarus" CountryPop="165k"/>
</x:Array>
<x:Array x:Key="StatusArray" Type="soDataGridProjectsHelpAttempt:ActivityStatus">
<soDataGridProjectsHelpAttempt:ActivityStatus VerbalStatus="Yes" BoolStatus="True"/>
<soDataGridProjectsHelpAttempt:ActivityStatus VerbalStatus="No" BoolStatus="False"/>
</x:Array>
</Window.Resources>
<Window.DataContext>
<soDataGridProjectsHelpAttempt:DataGridMainDataContext/>
</Window.DataContext>
<Grid>
<DataGrid ItemsSource="{Binding Collection}" AutoGenerateColumns="False" CanUserAddRows="True">
<DataGrid.Columns>
<DataGridTextColumn Width="Auto" Binding="{Binding UName}"/>
<DataGridComboBoxColumn Header="Country" DisplayMemberPath="CountryName"
ItemsSource="{StaticResource CountriesArray}" Width="Auto"
SelectedItemBinding="{Binding CountryData}"/>
<!--<DataGridComboBoxColumn Header="ActivityStatus" Width="Auto" ItemsSource="{StaticResource StatusArray}"
SelectedValueBinding="{Binding IsActive}" SelectedValuePath="BoolStatus" DisplayMemberPath="VerbalStatus"/>-->
<DataGridComboBoxColumn Header="ActivityStatus" SelectedItemBinding="{Binding IsActive, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<DataGridComboBoxColumn.ItemsSource>
<x:Array Type="system:Boolean">
<system:Boolean>True</system:Boolean>
<system:Boolean>False</system:Boolean>
</x:Array>
</DataGridComboBoxColumn.ItemsSource>
</DataGridComboBoxColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
DataGrid viewmodel:
public class DataGridMainDataContext
{
public DataGridMainDataContext()
{
Collection = new ObservableCollection(new List
{
new UserData
{
UName = "Greg",
IsActive = false,
},
new UserData
{
UName = "Joe",
IsActive = false,
},
new UserData
{
UName = "Iv",
IsActive = false,
}
});
}
public ObservableCollection<UserData> Collection { get; set; }
}
Models:
public class UserData : BaseObservableObject
{
private string _uName;
private object _countryData;
private bool _isActive;
public bool IsActive
{
get { return _isActive; }
set
{
_isActive = value;
OnPropertyChanged();
}
}
public string UName
{
get { return _uName; }
set
{
_uName = value;
OnPropertyChanged();
}
}
public object CountryData
{
get { return _countryData; }
set
{
_countryData = value;
OnPropertyChanged();
}
}
}
public class ActivityStatus:BaseObservableObject
{
private bool _boolStatus;
private string _verbalStatus;
public bool BoolStatus
{
get { return _boolStatus; }
set
{
_boolStatus = value;
OnPropertyChanged();
}
}
public string VerbalStatus
{
get { return _verbalStatus; }
set
{
_verbalStatus = value;
OnPropertyChanged();
}
}
}
public class Country : BaseObservableObject
{
private string _countryName;
private string _countryPop;
public string CountryName
{
get { return _countryName; }
set
{
_countryName = value;
OnPropertyChanged();
}
}
public string CountryPop
{
get { return _countryPop; }
set
{
_countryPop = value;
OnPropertyChanged();
}
}
public Country() { }
public Country(string n, string d)
{
this.CountryName = n;
this.CountryPop = d;
}
}
Hope it will help you.
regards,

dataGrid filter from combobox doesn't work

I m trying to filter a dataGrid by selecting two values from two comboBoxes, but the values selected send value 0 to the property.
var idShop = Shop; equal to zero
var idSupplier = Supplier; equal to zero
ViewModel
public class ConsultInvoiceViewModel:ViewModelBase
{
public Context ctx = new tContext();
private ICollectionView _dataGridCollection;
private string _filterString;
private ObservableCollection<Invoice> invoiceCollection = new ObservableCollection<Invoice>();
public ConsultInvoiceViewModel()
{
DataGridCollection = CollectionViewSource.GetDefaultView(Get());
//DataGridCollection.Filter = new Predicate<object>(Filter);
}
public ICollectionView DataGridCollection
{
get
{
return _dataGridCollection;
}
set
{
_dataGridCollection = value;
OnPropertyChanged("DataGridCollection"); }
}
private void FilterCollection()
{
if (_dataGridCollection != null)
{
_dataGridCollection.Refresh();
}
}
private void Search()
{
var idShop = Shop;
var idSupplier = Supplier;
var inv = (from i in ctx.Invoices
where i.shop == idShop
&& i.supplier == idSupplier
select i).SingleOrDefault();
invoiceCollection.Clear();
invoiceCollection.Add(inv);
FilterCollection();
}
private ObservableCollection<Invoice> Get()
{
ctx.Invoices.ToList().ForEach(invoice => ctx.Invoices.Local.Add(invoice));
invoiceCollection = ctx.Invoices.Local;
return invoiceCollection;
}
private void GetShop()
{
ctx.shops.ToList().ForEach(shop => ctx.shops.Local.Add(shop));
SShop = ctx.shops.Local;
}
private void GetSupplier()
{
ctx.foodSuppliers.ToList().ForEach(supplier => ctx.foodSuppliers.Local.Add(supplier));
FoodSupplier = ctx.foodSuppliers.Local;
}
private IList<foodSupplier> supplier;
public IList<foodSupplier> FoodSupplier
{
get
{
if (supplier == null)
GetSupplier();
return supplier;
}
set
{
supplier = value;
OnPropertyChanged("FoodSupplier");
}
}
private IList<shop> shop;
public IList<shop> SShop
{
get
{
if(shop == null)
GetShop();
return shop;
}
set
{
shop = value;
OnPropertyChanged("SShop");
}
}
private int _shop;
public int Shop
{
get
{
return _shop;
}
set
{
_shop = value;
OnPropertyChanged("Shop");
}
}
private int _supplier;
public int Supplier
{
get
{
return _supplier;
}
set
{
_supplier = value;
OnPropertyChanged("Supplier");
}
}
#region "Command"
private ICommand searchCommand;
public ICommand SearchCommand
{
get
{
return searchCommand ?? (searchCommand = new RelayCommand(p => this.Search(), p => this.CanSearch()));
}
}
private bool CanSearch()
{
if (Supplier != 0 && Shop != 0)
return true;
else
return false;
}
#endregion
}
View
<StackPanel Orientation="Horizontal">
<Label Content="Shop"/>
<ComboBox Name="shopComboBox"
Margin="5"
ItemsSource="{Binding SShop}"
DisplayMemberPath="shop1" Width="73"
SelectedItem="{Binding Shop, Mode=OneWayToSource}"
SelectedValuePath="idshop" SelectionChanged="shopComboBox_SelectionChanged"/>
<Label Content="Supplier"/>
<ComboBox Name="supplierComboBox"
Margin="5"
ItemsSource="{Binding FoodSupplier}"
DisplayMemberPath="supplier"
SelectedItem="{Binding Supplier, Mode=OneWayToSource}"
SelectedValuePath="idfoodSupplier"
Width="71"/>
<Label Content="Shop"/>
<Button Content="Search"
Margin="5"
Command="{Binding SearchCommand}"/>
</StackPanel>
<StackPanel Orientation="Vertical">
<DataGrid Margin="5" ItemsSource="{Binding DataGridCollection}" AutoGenerateColumns="False" >
<DataGrid.Columns>
<DataGridTextColumn Header="SuppNb" Binding="{Binding suppInvNumber}" Width="*"/>
<DataGridTextColumn Header="Shop" Binding="{Binding shop1.shop1}" Width="*"/>
<DataGridTextColumn Header="Date" Binding="{Binding date}" Width="*"/>
<DataGridTextColumn Header="Supplier" Binding="{Binding foodSupplier.supplier}" Width="*"/>
<DataGridTextColumn Header="Ref Supplier" Binding="{Binding refSupp}" Width="*"/>
<DataGridTextColumn Header="Unit" Binding="{Binding unit}" Width="*"/>
<DataGridTextColumn Header="Quantity" Binding="{Binding quantity}" Width="*"/>
<DataGridTextColumn Header="Prix/MOQ" Binding="{Binding unitPrice}" Width="*"/>
<DataGridTextColumn Header="Total Price" Binding="{Binding totalPrice}" Width="*"/>
</DataGrid.Columns>
</DataGrid>
</StackPanel>
It's wrong to bind at SelectedItem. Use SelectedValue with SelectedValuePath instead.
SelectedItem can only be binded to an item (object).
While SelectedValue can be binded to the value specified by the SelectedValuePath of the item.
Just change it like below and you should be able to get your result from the combobox.
<ComboBox Name="supplierComboBox"
Margin="5"
ItemsSource="{Binding FoodSupplier}"
DisplayMemberPath="supplier"
SelectedValue="{Binding Supplier, Mode=OneWayToSource}"
SelectedValuePath="idfoodSupplier"
Width="71"/>
You need to either bind Comoboxes to some command in your ViewModel, so that your ViewModel knows what you have selected ?
Or, you need to pass ComboBoxes selected items as parameters to Search command, so that in your Search method you can access User selected values.
Right now you are accessing Shop directly, which is meaningless. You are assuming that when you select an item in ShopComboBox, Shop variable in ViewModel will get that value. This is wrong.
When you bind say an ObservableCollection to DataGrid, and you select a row in DataGrid, ObservableCollection doesn't know anything about this selection but your DataGrid does.

Conditional CanUserAddRows in a DataGrid in WPF

Suppose I have a ComboBox like :
<ComboBox SelectedValue="{Binding DataContext.CanUserAddMultipleRows,
RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Page}},
Converter={StaticResource yesNoToBooleanConverter}}">
<ComboBoxItem>Yes</ComboBoxItem>
<ComboBoxItem>No</ComboBoxItem>
</ComboBox>
Here is the Converter :
public class YesNoToBooleanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (!(value == null || value == DependencyProperty.UnsetValue))
{
if ((bool)value == true)
{
return "Yes";
}
else
{
return "No";
}
}
else
{
return "No";
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (!(value == null || value == DependencyProperty.UnsetValue))
{
if (((ComboBoxItem)value).Content.ToString() == "Yes")
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
}
Now I have a DataGrid :
<DataGrid Grid.Row="7" Grid.Column="1" Grid.ColumnSpan="2" AutoGenerateColumns="False"
CanUserAddRows="{Binding DataContext.CanUserAddMultipleRows,
RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Page}}}"
ItemsSource="{Binding DataContext.MyObject,
RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Page}}}">
<DataGrid.Columns>
<DataGridTextColumn Header="Quantity" Binding="{Binding Quantity}"></DataGridTextColumn>
<DataGridTextColumn Header="Rate" Binding="{Binding Rate}"></DataGridTextColumn>
<DataGridTextColumn Header="Amount" Binding="{Binding Amount}"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
Now I want to provide 1 row by default to the users, so that if CanUserAddRows = false, then also they should be able to add 1 item to DataGrid. If CanUserAddRows = true, then user can have any number of rows he wants.
This thing might be simple but I am new to DataGrid. So, i asked this question.
In my example, there is a MayUserAddRows property of bool type. If MayUserAddRows == true then User can add as many records, but if MayUserAddRows == false then he will be able to fill only one record.
Also has CanUserAddRows property, which directly Binding with the property of DataGrid.CanUserAddRows.
Properties that are in the ViewModel implement the INotifyPropertyChanged interface via NotificationObject. He has an event PropertyChangedEventHandler(propertyName) which informs on notification properties. The key logic is here:
private void MyViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName.Equals("MayUserAddRows"))
{
// The MayUserAddRows property is changed
if (MyViewModel.MayUserAddRows == true)
{
// Allow to add a lot of records
MyViewModel.CanUserAddRows = true;
}
if (MyViewModel.MayUserAddRows == false)
{
// Prohibit the addition
MyViewModel.CanUserAddRows = false;
// And add the empty row
AddEmptyRow(MyViewModel.MyCollection);
}
}
}
Below is a full example:
XAML
<Window x:Class="ConditionalCanUserAddRows.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:this="clr-namespace:ConditionalCanUserAddRows"
WindowStartupLocation="CenterScreen"
Title="MainWindow" Height="300" Width="325">
<Grid>
<CheckBox Content="{Binding Path=IsChecked,
RelativeSource={RelativeSource Mode=Self}}"
ContentStringFormat="May user add rows - {0}"
HorizontalAlignment="Left"
VerticalAlignment="Top"
IsChecked="{Binding Path=MayUserAddRows}" />
<Button Content="Clear"
VerticalAlignment="Top"
HorizontalAlignment="Right"
Click="Clear_Click" />
<DataGrid Name="SimpleDataGrid"
Width="200"
Height="200"
CanUserResizeColumns="False"
AutoGenerateColumns="False"
RowHeaderWidth="0"
CanUserAddRows="{Binding Path=CanUserAddRows, Mode=TwoWay}"
ItemsSource="{Binding Path=MyCollection}">
<DataGrid.Columns>
<DataGridTextColumn Width="1.5*"
Header="Name"
Binding="{Binding Path=Name}" />
<DataGridTextColumn Header="Age"
Width="1.5*"
FontSize="14"
Binding="{Binding Path=Age}" />
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
Code-behind
public partial class MainWindow : Window
{
ViewModel MyViewModel = new ViewModel();
public MainWindow()
{
InitializeComponent();
this.DataContext = MyViewModel;
MyViewModel.MyCollection = new ObservableCollection<Person>();
MyViewModel.MyCollection.Add(new Person()
{
Age = 22,
Name = "Nick",
});
MyViewModel.MyCollection.Add(new Person()
{
Age = 11,
Name = "Sam",
});
MyViewModel.MyCollection.Add(new Person()
{
Name = "Kate",
Age = 15,
});
AddEmptyRow(MyViewModel.MyCollection);
MyViewModel.PropertyChanged += new PropertyChangedEventHandler(MyViewModel_PropertyChanged);
}
private void MyViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName.Equals("MayUserAddRows"))
{
if (MyViewModel.MayUserAddRows == true)
{
MyViewModel.CanUserAddRows = true;
}
if (MyViewModel.MayUserAddRows == false)
{
MyViewModel.CanUserAddRows = false;
AddEmptyRow(MyViewModel.MyCollection);
}
}
}
#region AddEmptyRow
private void AddEmptyRow(ObservableCollection<Person> collection)
{
collection.Add(new Person()
{
Name = "",
Age = 0,
});
}
#endregion
#region Clear
private void Clear_Click(object sender, RoutedEventArgs e)
{
MyViewModel.MyCollection.Clear();
}
#endregion
}
#region ViewModel
public class ViewModel : NotificationObject
{
#region MyCollection
public ObservableCollection<Person> MyCollection
{
get;
set;
}
#endregion
#region CanUserAddRows
private bool _canUserAddRows = false;
public bool CanUserAddRows
{
get
{
return _canUserAddRows;
}
set
{
_canUserAddRows = value;
NotifyPropertyChanged("CanUserAddRows");
}
}
#endregion
#region MayUserAddRows
private bool _mayUserAddRows = false;
public bool MayUserAddRows
{
get
{
return _mayUserAddRows;
}
set
{
_mayUserAddRows = value;
NotifyPropertyChanged("MayUserAddRows");
}
}
#endregion
}
#endregion
#region Model
public class Person
{
public string Name
{
get;
set;
}
public int Age
{
get;
set;
}
}
#endregion
#region NotificationObject
public class NotificationObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
#endregion
Output
MayUserAddRows="False"
MayUserAddRows="True"
Project compiled under the Visual Studio 2010, this example is fully fits the MVVM style.
This project available here

How to set the ItemSource of a Datagrid to a collection inside an object using MVVM

I have a ComboBox that holds a list of StrategyViewModels. The StrategyViewModel has an ObservableCollection of StrategyParameterViewModels inside of it. I have a StrategyViewModel called SelectedStrategy that I bound to the SelectedItem property on the combobox. When the user selects a Strategy from the ComboBox I would like to set the itemsource of a datagrid to the StrategyParameters inside that Strategy. I've tried all different ways, but nothing seems to work.
Here's the XAML:
<ComboBox Height="23" Margin="0,12,0,0" Name="cbxStrats" VerticalAlignment="Top" ItemsSource="{Binding Strategies}" DisplayMemberPath="Name" SelectedItem="{Binding Path=SelectedStrategy,Mode=TwoWay}" />
<DataGrid AutoGenerateColumns="False" Margin="12,97,14,35" Name="dgSettings" ItemsSource="{Binding SelectedStrategy.Parameters, Mode=TwoWay}">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Path=Name}" IsReadOnly="True"/>
<DataGridTextColumn Header="Value" Binding="{Binding Path=Value}" IsReadOnly="False"/>
</DataGrid.Columns>
</DataGrid>
Here is the Strategy ViewModel:
public class StrategyViewModel : ViewModelBase
{
public StrategyObject Strategy { get; set; }
public int Id
{
get { return Strategy.Id; }
set
{
if (value == Strategy.Id)
return;
Strategy.Id = value;
OnPropertyChanged("Id");
}
}
public string Name
{
get { return Strategy.Name; }
set
{
if (value == Strategy.Name)
return;
Strategy.Name = value;
OnPropertyChanged("Name");
}
}
public ObservableCollection<StrategyParameterViewModel> Parameters { get { return _parameters; } }
public ObservableCollection<StrategyParameterViewModel> _parameters;
public StrategyViewModel()
{
Strategy = new StrategyObject();
_parameters = new ObservableCollection<StrategyParameterViewModel>();
}
public StrategyViewModel(StrategyObject o, IEnumerable<StrategyParameterObject> pms)
{
Strategy = o;
_parameters = new ObservableCollection<StrategyParameterViewModel>();
foreach (StrategyParameterObject s in pms)
{
_parameters.Add(new StrategyParameterViewModel(s));
}
}
}
And here is the StrategyParameter ViewModel:
public class StrategyParameterViewModel : ViewModelBase
{
public StrategyParameterObject StrategyParameter { get; set; }
public int Id
{
get { return StrategyParameter.Id; }
set
{
if (value == StrategyParameter.Id)
return;
StrategyParameter.Id = value;
OnPropertyChanged("Id");
}
}
public int StrategyId
{
get { return StrategyParameter.StrategyId; }
set
{
if (value == StrategyParameter.StrategyId)
return;
StrategyParameter.StrategyId = value;
OnPropertyChanged("StrategyId");
}
}
public string Name
{
get { return StrategyParameter.Name; }
set
{
if (value == StrategyParameter.Name)
return;
StrategyParameter.Name = value;
OnPropertyChanged("Name");
}
}
public string Value
{
get { return StrategyParameter.Value; }
set
{
if (value == StrategyParameter.Value)
return;
StrategyParameter.Value = value;
OnPropertyChanged("Value");
}
}
public StrategyParameterViewModel()
{
StrategyParameter = new StrategyParameterObject();
}
public StrategyParameterViewModel(StrategyParameterObject o)
{
StrategyParameter = o;
}
}
The problem is that you are trying to set up a two way binding with a read-only property. You need to either add a setter for SelectedStrategy.Parameters, or use a OneWay binding on the datagrid itemssource.
Off the top of my head, I don't think you're doing anything wrong with regards to your XAML.
Data Binding is notoriously tricky. I suggest adding PresentationTraceSources to get more debug information as to what is being found. This will ship several lines of data about binding results to your Output window.
<ComboBox Height="23" Margin="0,12,0,0" Name="cbxStrats" VerticalAlignment="Top" DisplayMemberPath="Name">
<ComboBox.ItemsSource>
<Binding Path="Strategies" PresentationTraceSources.TraceLevel="High"/>
</ComboBox.ItemsSource>
<ComboBox.SelectedItem>
<Binding Path="SelectedStrategy" Mode="TwoWay" PresentationTraceSources.TraceLevel="High"/>
</ComboBox.SelectedItem>
</ComboBox>
<DataGrid AutoGenerateColumns="False" Margin="12,97,14,35" Name="dgSettings">
<DataGrid.ItemsSource>
<Binding Path="SelectedStrategy.Parameters" Mode="TwoWay" PresentationTraceSources.TraceLevel="High"/>
</DataGrid.ItemsSource>
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Path=Name}" IsReadOnly="True"/>
<DataGridTextColumn Header="Value" Binding="{Binding Path=Value}" IsReadOnly="False"/>
</DataGrid.Columns>
</DataGrid>
In addition, WPF Inspector can help, as you can tweak Data Binding expressions in real time when running your actual app. http://wpfinspector.codeplex.com/

Categories

Resources