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.
Related
I have a JibGrid DataGrid like so (JibGrid is an open-source relatively simple subclass of a standard DataGrid to implement stuff like filtering etc):
<dataGrid:JibGrid ItemsSource="{Binding AvailableRDs}"
FilteredItemsSource="{Binding AvailableRDs}"
SelectedItem="{Binding SelectedAvailRD}"
AutoGenerateColumns="False" >
<dataGrid:JibGrid.Columns>
<DataGridCheckBoxColumn Header="Add?" Binding="{Binding Add}" Visibility="{Binding GetAddVisibility}"/>
<DataGridTextColumn Header="Tag" Binding="{Binding Tag}" />
<DataGridTextColumn Header="Revision Tag" Binding="{Binding RevisionTag}" />
<DataGridTextColumn Header="Current System" Binding="{Binding SystemStr}" />
</dataGrid:JibGrid.Columns>
</dataGrid:JibGrid>
The intent is that there is a custom class for each row that provides properties for the content of each cell in that row - Tag, RevisionTag, etc. All of that works fine. What I can't get to work is that Visibility binding. I would like for the checkbox in each row to be invisible if the CanAdd property on the class representing that Row in the DataContext returns False. When I add the Visibility binding in XAML as above, it seems that what happens is that WPF attempts to apply this binding to the actual column instead of for each row, so the binding fails to connect. I can go in using WPF explorer and manually bind a row's checkbox visibility DependencyProperty to the CanAdd property of the Row's datacontext, and that works fine, but I can't figure out how to, in XAML or code, cause it to generate that binding for the checkbox in every row automatically. Anybody have any ideas on that?
I have searched around for questions like this, and it seems that, for some reason, everybody wants to change the visibility of the column itself based on something in the datacontext of the whole grid, and nobody else wants to change the visibility of things in individual rows based on that row's datacontext. I tried the solution here while I was trying to figure this out, and that's what that answer is trying to do.
You can use DataGridTemplateColumn and BooleanToVisibilityConverter to achieve the desired result
Add BooleanToVisibilityConverter to your resources
<Window.Resources>
<BooleanToVisibilityConverter x:Key="BoolToVis"/>
</Window.Resources>
Set Converter for the DataGridTemplateColumn binding
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Add}"
Visibility="{Binding CanAdd, Converter={StaticResource BoolToVis}}" ></CheckBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
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 ...}"
...
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
I wish to have a custom column type in a WPF datagrid, part of which will be a textbox for user input. Unfortunately, it does not appear to inherit the look and feel of the datagrid itself- it doesn't show the alternating colour, when a row is selected or edited the cell in question doesn't highlight in the same way, and so on.
<DataGridTemplateColumn Header="Name" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate >
<TextBox Text="{Binding DisplayName,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" IsReadOnly="False" BorderThickness="0" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
It looks like the style of a default textbox overrides that of the datagrid; is there a way just to use the datagrids style? I could of course style the textbox to mimic the datagrid, but if I wish to add other controls I'd have to do that for each one as well. If I do go down that route, how would I change the style based on properties of the datagridrow from within the celltemplate?- for example IsSelected.
Please change your XAML to add the following to your textbox definition:
BorderThickness="0"
Background="{Binding RelativeSource={RelativeSource AncestorType=DataGridRow}, Path=Background}"
This will make the Textbox inherit your datagrid backround property.
Good Luck
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>