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 ...}"
...
Related
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.
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 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
How can I display a button in a DataGridTemplateColumn only when its row is selected? I've tried this, but of course there is no IsSelected available to me. It doesn't make sense to have an IsSelected property on the entity to which the rows are bound, and even so I wouldn't want to have to couple my DataGrid to my model that tightly. Is there anyway that the interface can handle this itself?
This is what I have:
<sdk:DataGrid Name="_categorySummaryDataGrid"
MinHeight="200"
ItemsSource="{Binding ElementName=_userControl, Path=CategorySummaries}"
AutoGenerateColumns="False" RowDetailsVisibilityMode="VisibleWhenSelected">
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn x:Name="_nameColumn" Binding="{Binding Path=Name}" Header="Name" Width="Auto" IsReadOnly="True" />
<sdk:DataGridTextColumn x:Name="_descriptionColumn" Binding="{Binding Path=Description}" Header="Description" Width="*" IsReadOnly="True" />
<sdk:DataGridTemplateColumn x:Name="_detailsColumn" Width="Auto">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="..." ToolTipService.ToolTip="View Category Details"
Visibility="{Binding Path=IsSelected, Converter={StaticResource BooleanToVisibility}}"/>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
</sdk:DataGrid.Columns>
</sdk:DataGrid>
For design reasons, I want the button to only show when the row that contains it is selected. And, if possible, I want it to show in a different way when the row is being hovered over.
There seem to be more limitations to Silverlight and WPF that I had hoped. I hope that these things are possible. Thanks. :)
Edit:
I do not have, and will not be getting Expression Blend. Thank you, that is all.
Whenever you want to change the visual appearance of anything in Silverlight, you have to think in terms of VisualStateManager. To change the appearance of a Selected DataGrid cell, you'll need to avail yourself to the wonders of VSM. By editing DataGridColumn.CellStyle's "Selected" state you can change the appearance of the selected grid cell.
In Blend, drag and drop a new DataGrid on a Page.
Right click on the DataGrid and choose "Add Column --> Add DataGridTemplateColumn"
Right click on the DataGridTemplateColumn in the "Object and timeline" pane and go "Edit Column Styles --> Edit CellStyle --> Edit Copy"
Right click on "Style" in the "objects and timeline" pane and go "Edit Template --> Edit Current".
Now comes the interesting part, how to specifically solve the problem of a disappearing button. If the button is the only thing you've got in that CellTemplate then you can just hide the whole contents of the cell.
Select the "Selected" state from the "States" pane.
Set the visibility of the ContentPresenter in the CellStyle.Template to Collapsed. (A nicer UX would be to add a 0.3seconds animation taking opacity from 100% to 0%).
Essentially this whole tutorial is about getting to the CellStyle.Template for your DataGridColumn and adding an animation to the Selected State.