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 you pass parameters from xaml?

Your constructor: public ClockControl(String city) { InitializeComponent(); this.initController(); //… } First of all, if you want to use ClockControl from XAML, then you need a default constructor, means a constructor which doesn’t take any parameter. So the above constructor is not going to work. I would suggest you to define a property with name City, … Read more

How can I tell my DataTemplate to bind to a property in the PARENT ViewModel?

The answer is this: <DataTemplate x:Key=”CodeGenerationMenuTemplate”> <MenuItem Header=”{Binding Title}” Command=”{Binding DataContext.SwitchPageCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Menu}}}” CommandParameter=”{Binding Title}”/> </DataTemplate> I just saw that Nir had given me the syntax to solve the above issue on this question: What is the best way in MVVM to build a menu that displays various pages?.

How to bind DataTemplate datatype to interface?

You can bind to interfaces by telling wpf explicitly that you are binding to an interface field: (Please note that ViewModelBase is simply a base-class that implements the INotifyPropertyChanged interface) public class Implementation : ViewModelBase, IInterface { private string textField; public string TextField { get { return textField; } set { if (value == textField) … Read more

WPF – converting Bitmap to ImageSource

For others, this works: //If you get ‘dllimport unknown’-, then add ‘using System.Runtime.InteropServices;’ [DllImport(“gdi32.dll”, EntryPoint = “DeleteObject”)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool DeleteObject([In] IntPtr hObject); public ImageSource ImageSourceFromBitmap(Bitmap bmp) { var handle = bmp.GetHbitmap(); try { return Imaging.CreateBitmapSourceFromHBitmap(handle, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); } finally { DeleteObject(handle); } }

How to get TreeViewItem from HierarchicalDataTemplate item?

TreeViewItem item = (TreeViewItem)(mainTreeList .ItemContainerGenerator .ContainerFromIndex(mainTreeList.Items.CurrentPosition)); DOES NOT WORK (for me) as mainTreeList.Items.CurrentPosition in a treeview using a HierarchicalDataTemplate will always be -1. NEITHER DOES below as as mainTreeList.Items.CurrentItem in a treeview using a HierarchicalDataTemplate will always be null. TreeViewItem item = (TreeViewItem)mainTreeList .ItemContainerGenerator .ContainerFromItem(mainTreeList.Items.CurrentItem); INSTEAD I had to set a the last selected TreeViewItem … 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

Get the item doubleclick event of listview

<ListView.ItemContainerStyle> <Style TargetType=”ListViewItem”> <EventSetter Event=”MouseDoubleClick” Handler=”listViewItem_MouseDoubleClick” /> </Style> </ListView.ItemContainerStyle> The only difficulty then is if you are interested in the underlying object the listviewitem maps to e.g. private void listViewItem_MouseDoubleClick(object sender, MouseButtonEventArgs e) { ListViewItem item = sender as ListViewItem; object obj = item.Content; }

How to scroll to the bottom of a ScrollViewer automatically with Xaml and binding?

You can either create an attached property or a behavior to achieve what you want without using code behind. Either way you will still need to write some code. Here is an example of using attached property. Attached Property public static class Helper { public static bool GetAutoScroll(DependencyObject obj) { return (bool)obj.GetValue(AutoScrollProperty); } public static … Read more