WPF Datagrid Binding to Combobox and Textbox
I need your help with WPF Datagrid. I looked at some answered questions in platform and made something similar, but cant resolve some problems.
I have struct named as Table
public struct Table
{
public string Name { get; set; }
public int Value { get; set; }
}
and Class named as Room
public class Room : INotifyPropertyChanged
{
public string strRoomlName;
public string Name
{
get { return strRoomlName; }
set { strRoomlName = value; }
}
public ObservableCollection<Table> tbTables = new ObservableCollection<Table>();
public ObservableCollection<Table> PrpTables
{
get { return tbTables; }
set
{
tbTables = value;
OnPropertyChanged("PrRawValues");
if (value != null && value.Count > 0)
{
PrpSelectedTable = value.First();
}
}
}
private Table selectedTable;
public Table PrpSelectedTable
{
get
{
return selectedTable;
}
set
{
selectedTable = value;
OnPropertyChanged("PrpSelectedTable");
ComboValue = value.Value;
OnPropertyChanged("ComboValue");
}
}
public int ComboValue
{
get;
set;
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
And Main Class as follows,
public partial class MainWindow : Window
{
ObservableCollection<Room> rooms;
public MainWindow()
{
InitializeComponent();
rooms = new ObservableCollection<Room>();
Table t1 = new Table { Name = "Table #1", Value = 0 };
Table t2 = new Table { Name = "Table #2", Value = 3 };
Table t3 = new Table { Name = "Table #3", Value = 5 };
Table t4 = new Table { Name = "Table #4", Value = 2 };
Room rm1 = new Room();
rm1.Name = "Small Hall";
rm1.tbTables.Add(t1);
rm1.tbTables.Add(t2);
rm1.tbTables.Add(t3);
rm1.tbTables.Add(t4);
rooms.Add(rm1);
Table t21 = new Table { Name = "Table #1", Value = 0 };
Table t22 = new Table { Name = "Table #2", Value = 3 };
Table t23 = new Table { Name = "Table #3", Value = 5 };
Table t24 = new Table { Name = "Table #4", Value = 12 };
Table t25 = new Table { Name = "Table #5", Value = 8 };
Table t26 = new Table { Name = "Table #6", Value = 3 };
Table t27 = new Table { Name = "Table #7", Value = 20 };
Room rm2 = new Room();
rm2.Name = "Main Hall";
rm2.tbTables.Add(t21);
rm2.tbTables.Add(t22);
rm2.tbTables.Add(t23);
rm2.tbTables.Add(t24);
rm2.tbTables.Add(t25);
rm2.tbTables.Add(t26);
rm2.tbTables.Add(t27);
rooms.Add(rm2);
this.gridSignals.DataContext = rooms;
}
XAML Layout:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Custom="http://schemas.microsoft.com/winfx/2006/xaml/presentation/ribbon" x:Class="BindingReiview.MainWindow"
Title="MainWindow" Height="320" Width="396">
<StackPanel Height="266" VerticalAlignment="Top" Margin="0,0,2,0">
<DataGrid ItemsSource="{Binding}" Name="gridSignals" CanUserAddRows="False" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header=" Room " Binding="{Binding Name}" Width="110"/>
<DataGridTemplateColumn Header=" Tables " Width="100" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding PrpTables, Mode=TwoWay}" SelectedIndex="0" SelectedItem="{Binding PrpSelectedTable, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
DisplayMemberPath="Name" Name="cmbVal" ></ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header=" Geust(s)" Width="100">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding ComboValue}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</StackPanel>
ObservableCollection is binded to DataSource of grid. Combobox items are binded to Room, where there is ObservableCollection. When Table is selected Number of Guests are shown to Textbox. Because Textbox text is binded to ComboValue (Combobox.SelectedItem).
So, I need to change selected table's guests. For example(in picture), in Table #5 Value is 8, and it needs to be changed by editing textbox.
Any help and suggestions are welcomed!
Are you looking to update the value in the variable property Value
in the selected table (variable t25
in your code/example picture)? In that case you could just bind directly to that value.
{Binding PrpSelectedTable.Value, Mode=TwoWay}
That will update the value in the selected table. I would also suggest making Table implement the INotifyPropertyChanged
interface. That way if the Value or Name property of the Table changes the display will automatically update. Hopefully that helps.