WPF Stackpanel inside DataGrid Cell - wpf

I have got a WPF Application, which contains a Window, inside it there is a DataGrid. On Startup, the DataGrid is completely empty, its Columns are created at runtime by code.
I'm binding to a DataTable. The first coloumn is a single Text Field. I need to show one more Cell per Row, but as a Stackpanel, which itself holds some UserControls.
Actually I'm trying to first insert a blank column as placeholder for my StackPanel and later insert a StackPanel from Code to each Cell.
Unfortunately i don't get it running for some reasons. I cannot put a new Item to the Cell. Can anybody help me, please?
Best Regards,
Jonas

I would recommend to design and fill the datagrid via XAML.
YourItems is the Collection of object which you want to show
userControlColumn is the additional column for your usercontrol
firstColumn is your first DataGridTextColumn
<DataGrid x:Name="myGrid" ItemsSource="{Binding YourItems}">
<DataGrid.Columns>
<DataGridTemplateColumn x:Name="userControlColumn" Header="Column1">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<ns:YourCustomControl/>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn x:Name="firstColumn" Header="Columns2" Binding="{Binding YourDesiredProperty}"/>
</DataGrid.Columns>
</DataGrid>

Try and use a DataGridTemplateColumn, then you can put a stackpanel in each cell

Related

WPF autocomplete datagridtextcolumn

I have a datagrid in my application (sales invoice form) with five columns. One column needs to be autocompletebox control. I'd like to implement the whole thing using the MVVM pattern.
How can i solve the issue..
I have added the AutoCompleteBox column to the datagrid.
Have the DataTemplate in resources.
<DataTemplate x:Key="AutoCompleteTemplate">
<tool:AutoCompleteBox ItemsSource="{Binding Source}"/>
</DataTemplate>
<DataGrid>
<DataGrid.Columns>
<DataGridTemplateColumn Header="AutoCompleteColumn" CellTemplate="{StaticResource AutoCompleteTemplate}"/>
<DataGridCheckBoxColumn Header="CheckBoxColumn"/>
</DataGrid.Columns>
if the columns in the Datagrid are created dynamically, then you may need to create the column at the AutoGeneratingColumn (use interactiontrigger for MVVM) event based on some conditions that you have. Create a DataGridTemplateColumn and set the CellTemplate by finding from the resources.

WPF - DataGrid - Hiding DataGridCeckBoxColumn via DataTrigger

I've got a DataGrid in wpf with different columns. I want to change the property "Visibility" of one explicit column via datatrigger, but a cant access the "Style" property.
How can i collapse or hide the hole column?
<DataGrid.Columns>
<DataGridCheckBoxColumn Binding="{Binding IsChanged, Mode=OneWay}"
Header="Changed"
CanUserSort="False">
</DataGridCheckBoxColumn>
<!--more columns-->
</DataGrid.Columns>
DataGridRow and DataGridCell have Styles, DataGridColumn doesn't. I'm guessing this is because rows and cells are the only things being displayed in the UI. Columns are only used by DataGrid internally to keep track of its rows and cells and their content.
Conveniently, columns do have a Visibility property though, which you can bind on each specific column:
<DataGridCheckBoxColumn Visibility="{Binding ...}"
...

Replacing checkboxes with images in Xceed's WPF DataGrid

I'm using the MVVM pattern with Xceed's WPF DataGridControl and I've bound a column of my grid to a boolean property on my view-model/data-context.
By default it displays as a checkbox, instead I'd like to display an image e.g. a smile face for True and a sad face for False. It doesn't need to behave like a checkbox as the column is read-only.
What is the best way to achieve this?
There was a blog posted on Xceeds website about how to style a DataCell based on other values. So essentially, you can create a DataTemplate with an image control in it and you can create a condition where you set the happy face if the value is true and a sad face if the value is false. Here is the following link that shows how to do this:
http://xceed.com/CS/blogs/techside/archive/2011/07/06/datacell-styling-vs-cellcontenttemplate.aspx
you have to create data grid template column to achieve custom style.
<DataGrid.Columns>
<DataGridTemplateColumn Header="First Name" IsReadOnly="True" Width="Auto" MinWidth="100" CanUserSort="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid>
<Image Source="smile.jpg"/>
<Image Source="smile.jpg"/>
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
you can further use datatriggers to play with visibility of images

Copy doesn't work on DataGrid templated columns?

When working with templated column in WPF datagrid, you cann't copy cell content anymore, does anyone knows how this can be solved?
You need to set the ClipboardContentBinding property to the desired property in the row item, see the following code:
<DataGrid>
<DataGrid.Columns>
<DataGridTemplateColumn ClipboardContentBinding="{Binding YouProperty}">
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>

WPF Datagrid Template column edit event and alternating column color

I have been using WPF for quiet sometime. I know DataGrid in WPF does not have a Column collection as dependency property so columns cannot be added dynamically.
The application I am developing is highly dynamic so the number of columns are not known. So I am creating DataGridTemplate columns from code behind.
Problem 1 : I want alternating columns to have different background color. How do I do it programatically?? (DataGridTemplateColumn doesnot have a Background property so I am not able to figure out a solution)
Problem 2 : My DataGridTemplateColumn has a DataTemplate in which I have a StackPanel with 2 TextBoxes in it. There is an event in DataGrid called CellEditing Event which fires when we edit a cell. It works for default column, but for my column if I edit those TextBoxes, the event is snot getting fired!!! So how do I achieve it??
(I sometimes get amazed by WPF !!!)
Problem Zero You can have the columns in a datagrid generated for you if you use AutoGenerateColumns="true" when you set up your datagrid. It won't add columns dynamically later, but it might if you reset the itemssource? (not positive on that one)
Problem One DataGrid has properties AlternatingRowBackground and AlternationCount to set up alternating row backgrounds. But i don't see anything for alternating column backgrounds in the grid itself. You could do it inside your datatemplate, though:
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Background="Red" Foreground="White">I'm Red</TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
But i still see a margin inside that even with margin=0, so the cells look funny if you use any really obvious colors.
Problem Two do you mean the CellEndEditing event? Because i don't see any other cell editing events. I tried the following:
<DataGrid ItemsSource="{Binding}" AutoGenerateColumns="False" CellEditEnding="DataGrid_CellEditEnding">
<DataGrid.Columns>
<DataGridTextColumn Header="A" Binding="{Binding Field0}" />
<DataGridTemplateColumn Header="BC">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Field1}"/>
<TextBlock Text="{Binding Field2}" />
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBox Text="{Binding Field1}"/>
<TextBox Text="{Binding Field2}" />
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
And my DataGrid_CellEditEnding event handler gets called whenever either of the textboxes in the CellEditingTemplate lose focus, whether data changed or not, so things appear to be working for me.
Are you using some other DataGrid than the "built in" WPF one?

Resources