.NET/C# Binding Custom DependencyProperty to ViewModel Property

Consider the following scenario: UserControl with a custom DependencyProperty ViewModel with an exposed property (using INotifyPropertyChanged) Goal: create two-way data binding between these two properties (i.e., ViewModel property gets updated if DependencyProperty changes and vice versa) ViewModel (implements INotifyPropertyChanged): private…

Read More

.NET/C# Get First Visible Item of a ListBox

This is some code to get the first visible item in a WPF ListBox whenever the user scrolled. In XAML: <ListBox Name="MyListBox" ItemsSource="{Binding SomeCollection}" ScrollViewer.ScrollChanged="ScrollChanged"/> In the code-behind: private void ScrollChanged(object sender, ScrollChangedEventArgs e) { VirtualizingStackPanel panel = FindVisualChild<VirtualizingStackPanel>(MyListBox); if…

Read More

.NET/C# CSV Export of Properties

Export an object’s properties as comma separated values (CSV) using reflection. public static string GetCsvHeaderString() { return String.Join(",", typeof(MyClass).GetProperties(). Where<PropertyInfo>(x => x.CanRead). Select<PropertyInfo, string> (x => x.Name).ToArray<string>()); }   public string ToCsvString() { return String.Join(",", GetType().GetProperties(). Where<PropertyInfo>(x => x.CanRead). Select<PropertyInfo,…

Read More