I have this XAML that selects a value from a combobox that the ItemSource is a Enum. The tutorial I used is:
http://www.c-sharpcorner.com/uploadfile/dpatra/combobox-in-datagrid-in-wpf/
<DataGrid x:Name="dgProductItem"
ItemsSource="{Binding ProductVersion.ProductItems}"
<DataGridTemplateColumn Header="Deployment Type" Width="120">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding DeploymentType}"></TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Source={StaticResource DeploymentTypeEnum}}"
SelectedItem="{Binding DeploymentType}">
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
However when I change a value from one row, it will update all the rows. Does anyone know why this is?
Edit:
if I just change one row, it will only update that row, but when I go to change a different row, that row I just changed will also change the previous one..
Cheers
Apologies for the duplicates but after a few hours of guessing because there isn't enough material on the web for this kinda stuff, the solution is:
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Source={StaticResource DeploymentTypeEnum}}"
SelectedItem="{Binding DeploymentType}"
**IsSynchronizedWithCurrentItem="false**">
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
IsSynchronizedWithCurrentItem - does what it says on the tin. However, when you select an item, the current one will disappear but at least it won't update all rows.
Related
I have a little bit of a quandary that I haven't been able to resolve, at least not in a method that makes sense from an MVVM standpoint.
I have a datagrid that contains client-employee data with start and end dates for each client/employee relationship.
The DataGrid overall ItemsSource is the ClientToEmp CollectionViewSource bound to the ClientToEmpObservableCollection. However, in the combobox column that allows them to change/update the current employee the ItemsSource is the Employee CollectionViewSource bound to the User ObservableCollection(ie, a list of all employees they can choose for this client).
This part works fine, when I click the combobox, the proper employees are displayed to select from in the combobox. However when the datagrid loads, I want the CurrentEmp from the ClientToEmp cvs to be showing as the selected employee(ie, the employee that is currently assigned to this client). When they click on it, they should be able to change the Employee(from the separate employee cvs), which would then update the value in the ClientToEmp cvs.
<DataGrid Name="ClientToEmpMDG" ItemsSource="{Binding cvsClientToEmp}"
AutoGenerateColumns="False" AutoGeneratingColumn="Gen_AutoGridColumns">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding ClientName}" Header="Client Name" IsReadOnly="True"/>
<DataGridTemplateColumn Header="Current Emp">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Path=DataContext.cvsEmp, RelativeSource={RelativeSource AncestorType=Window}}"
DisplayMemberPath="DisplayName"
SelectedValuePath="User_ID"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Start Date">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<DatePicker Name="StartDateDP" SelectedDate="{Binding Path=Start_Date}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="End Date">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<DatePicker Name="EndDateDP" SelectedDate="{Binding Path=End_Date}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
How can I get the current employee to show properly on load? The selected value should match the User_ID from the two ObservableCollections. If I use the SelectedValue Property, all comboboxes in the grid show the same value, and it still doesn't bind properly to the cvsClientToEmp value I want it to bind to. I want each combobox to have it's own value, not all of them to share a single value.
UPDATE: I fixed the issue with all of the comboboxes displaying the same value by changing adding "IsSynchronizedWithCurrentItem" to False...Still cannot get it to bind to the value coming back from the DB as to who the current employee is
You are missing SelectedValue in your ComboBox
<ComboBox ItemsSource="{Binding Path=DataContext.cvsEmp, RelativeSource={RelativeSource AncestorType=Window}}"
DisplayMemberPath="DisplayName"
SelectedValue="{Binding CurrentEmp}"
SelectedValuePath="User_ID"/>
See also https://stackoverflow.com/a/4902454/10718884 for a good explanation
Note: There is also a DataGridComboBoxColumn
<DataGridComboBoxColumn
ItemsSource="{Binding Path=DataContext.cvsEmp, RelativeSource={RelativeSource AncestorType=Window}}"
DisplayMemberPath="DisplayName"
SelectedValueBinding="{Binding CurrentEmp}"
SelectedValuePath="User_ID">
</DataGridComboBoxColumn>
I am trying to make a DataGrid in WPF that will display the different properties of the items in a given collection. So far, everything works perfectly, except for one. Here is an image of my DataGrid, which is fully populated just as intended:
And you may already see my problem. Each and every item has a value associated with it, which is properly binded. However, I cannot see the value for any of the items unless I specifically click on that cell. For example:
I want the values to be visible at all times, just like the Name and Default Value Columns, but it is very important that the value, and only the value, is editable from the DataGrid at runtime. Here is the XAML I have in place for the datagrid:
<DataGrid Margin="20" AutoGenerateColumns="False" ItemsSource="{Binding ConfigurationParameterCollection}" Name="MasterListBox" ScrollViewer.VerticalScrollBarVisibility="Visible" SelectionChanged="MasterListBox_SelectionChanged" AlternationCount="2" DockPanel.Dock="Top" HorizontalAlignment="Center" Width="1000" >
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
<!-- Begin Problem Area-->
<DataGridTemplateColumn Header="Value">
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=Value, Mode=TwoWay}" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
<!-- End Problem Area-->
<DataGridTextColumn Header="Default Value" Binding="{Binding DefaultValue}"/>
</DataGrid.Columns>
</DataGrid>
Don't worry about the name of the DataGrid, it used to be a ListBox and I just never bothered to change the name. Everything links up perfectly, I just can't figure out why the TextBox text is not visible.
Any help you can give is much appreciated.
EDIT
I know that I can achieve this with using CellTemplate instead of CellEditingTemplate, but the latter has the feature of only editing on two clicks, but selecting the row otherwise. I want to have this feature, so If there is a way to do that with CellTemplate please let me know. With CellTemplate, I see the boxes at all times:
I want it to be like in the second picture, where the rest of the column appears in style with the datagrid, while the cell I am editing looks like a text box.
Change
DataGridTemplateColumn.CellEditingTemplate
To
DataGridTemplateColumn.CellTemplate
You can specify a display template, and an edit template ...
<DataGridTemplateColumn Header="Value">
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=Value, Mode=TwoWay}" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Value, Mode=TwoWay}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
After wasting 2 day, I just little understand of wpf datagrid data-binding process.In the Earlier,I want to show combo box in datagrid view column.After googled, i found the way to bind combo in datagrid.Now i got the problem that looks like easy but it make me crazy.
This is Initial state.In this state,Combo box is missing.
After double click the row,it visible.
How can i show combo box in Initial state? Thank You.
There'are two templates used in DataGrid:
CellTemplate
CellEditingTemplate
CellTemplate acts a role of, as you said, initial state, whereas CellEditingTemplate is used when you edit a cell. For instance, DataGridTextColumn is actually looks like this:
<DataGrid x:Name="dataGrid" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding SomeField}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox Text="{Binding SomeField}"/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
Recently I asked similar questions here and here, but could not fix the issue properly.
I have a DataGrid with ComboBox that can contain either a selected item or not. But if it does then the ComboBox should select it when the dropdown is opening which is doesn't.
Currently I have this code which works except when opening the dropdown the first time. It is nothing selected.
<DataGridTemplateColumn Header="Company">
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=DataContext.Companies}"
SelectedItem="{Binding Company, Converter={StaticResource NullValueConverter}}"/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Company, Converter={StaticResource NullValueConverter}}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
The NullValueConverter prevents exceptions if the Company is null.
If your company was not a string as you mentioned in the comments then it can't display the Name unless you set
DisplayMemberPath="Name" or create an ItemTemplate.
You should use SelectedItem="{Binding
Company,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" to update
the value as soon as it changes because if you don't it will only
raise ProperyChanged when you focus another cell or row.
if your Property was immutable or a value type then you
should use SelectedValue="{Binding Path=Company,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" SelectedValuePath="Content"
.
In Old Ver. of My code I am binding
dgvShow.Columns["grdTest_Active"].DataPropertyName = oDTList.Columns["STATUS"].ToString();
dgvShow.Columns["grdTest_LnkOrder"].DataPropertyName = oDTList.Columns["ORDER"].ToString();
I am converting the code in WPF and I have taken a grid and in that I have placed
<DataGridTemplateColumn Header="Active" Width="50" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox x:Name="chkActive" IsChecked="{Binding Path=Active}" HorizontalAlignment="Center" VerticalAlignment="Center" IsEnabled="True"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Order" Width="60" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Path=Order}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
when I am using
DataGridObjectTypeList.DataContext = oDTList.DefaultView;
on vmUCObjType Cs file It is binding the grid but, checkbox and combobox data is not showing.
I want to bind oDTList datatable value to my checkbox and combobox how to do that?
Please some one help me I am new in WPF.
In your XAML file, write
<ComboBox x:Name="Name_ComboBox" DisplayMemberPath="Name" SelectedValuePath="id"/>
In your code behind, put:
Name_ComboBox.ItemSource = Table_name;
(Table_name being a reference to the table you mentioned).
then you can reach the id of the currently selected person in the combo box using the expression:
Name_ComboBox.SelectedValue
I have removed ComboBoxColumn and added DataGridComboBoxColumn in front end and in code behind I have done something like this:
((DataGridComboBoxColumn)dgShowList.Columns[1]).ItemsSource = DTCount.DefaultView;
((DataGridComboBoxColumn)dgShowList.Columns[1]).DisplayMemberPath = DTCount.Columns["Order"].ToString();
((DataGridComboBoxColumn)dgShowList.Columns[1]).SelectedValuePath = DTCount.Columns["Order"].ToString();
((DataGridComboBoxColumn)dgShowList.Columns[1]).SelectedValueBinding = new Binding("Order");