Sort a wpf datagrid programmatically

voo’s solution was not working for me, ItemsSource was null, most likely because it was not directly set, but bound. All other solutions I found here at StackOverflow were dealing with sorting the Model only, but the DataGrid header was not reflecting to the sort. Here’s a proper solution based on the incomplete script here: … Read more

How do I dynamically generate columns in a WPF DataGrid?

Ultimately I needed to do two things: Generate the columns manually from the list of properties returned by the query Set up a DataBinding object After that the built-in data binding kicked in and worked fine and didn’t seem to have any issue getting the property values out of the ExpandoObject. <DataGrid AutoGenerateColumns=”False” ItemsSource=”{Binding Results}” … Read more

Add context menu in datagrid, how to get the select Item value

Expanding on Bolu’s comment you could use SelectedItem to get the current item. Below is a quick example: <DataGrid ItemsSource=”{Binding Source}” SelectedItem=”{Binding SelectedItemProperty, Mode=TwoWay}”> <DataGrid.ContextMenu> <ContextMenu> <MenuItem Command=”{Binding MyCommand}” Header=”MyCommand”/> </ContextMenu> </DataGrid.ContextMenu> <DataGrid.Columns> <DataGridTextColumn Header=”Name” Binding=”{Binding Key, Mode=TwoWay}” Width=”1*”/> <DataGridTextColumn Header=”Value” Binding=”{Binding Value, Mode=TwoWay}” Width=”3*”/> </DataGrid.Columns> </DataGrid> SelectedItem is now bound to SelectedItemProperty in the … Read more

How to bind column header to property in ViewModel? (WPF MVVM)

Unfortunately, the column definitions of the DataGrid don’t inherit the DataContext, because they’re not part of the visual tree, so you can’t bind directly to the ViewModel. You need to resort to a workaround such as the one described in this article: <DataGrid.Resources> <local:BindingProxy x:Key=”proxy” Data=”{Binding}” /> </DataGrid.Resources> … <DataGridTextColumn Header=”{Binding Data.MyTitle, Source={StaticResource proxy}}”/>

How to set background of a datagrid cell during AutoGeneratingColumn event depending on its value?

I can propose two different solutions for your question: the first is “code-behind-style” (which you are asking for but personally I think it is not right approach in WPF) and more WPF-style (which more tricky but keeps code-behind clean and utilizes styles, triggers and converters) Solution 1. Event handling and code-behind logic for coloring First … Read more

Binding DynamicObject to a DataGrid with automatic column generation?

There is no uniform way to query dynamic properties, generally it’s expected that you know them ahead of time. With DynamicObject, implementers may override GetMemberNames and that generally gives you the properties, however it is really meant for debugging because there is no requirement that it provide all properties. Otherwise if it’s your own DynamicObject … Read more

WPF DataGrid – Event for New Rows?

The event you are looking for is DataGrid.AddingNewItem event. This event will allow you to configure the new object as you want it and then apply it to the NewItem property of the AddingNewItemEventArgs. XAML: <DataGrid Name=”GrdBallPenetrations” ItemsSource=”{Binding BallPenetrationCollection}” SelectedItem=”{Binding CurrentSelectedBallPenetration}” AutoGenerateColumns=”False” IsReadOnly=”False” CanUserAddRows=”True” CanUserDeleteRows=”True” IsSynchronizedWithCurrentItem=”True” AddingNewItem=”GrdBallPenetrations_AddingNewItem”> Code behind in C#: private void GrdBallPenetrations_AddingNewItem(object sender, … Read more

Binding datagrid column width

Because DataGrid columns are abstract objects which do not appear in the logical or visual tree of your window. You cannot bind properties on them using ElementName (there will be no namescope which is needed for those bindings). You can try using Source and x:Reference instead, e.g. {Binding Source={x:Reference TextCol01}, Path=ActualWidth}