How do you bind a CollectionContainer to a collection in a view model?

The CompositeCollection has no DataContext, the bindings in the CollectionContainers will not work if they bind directly to a property (which implicitly uses the DataContext as source). You need to explicitly specify a source, i would suggest you name the control with your DataContext and use x:Reference to get it (ElementName will not work) or … Read more

Pushing read-only GUI properties back into ViewModel

Yes, I’ve done this in the past with the ActualWidth and ActualHeight properties, both of which are read-only. I created an attached behavior that has ObservedWidth and ObservedHeight attached properties. It also has an Observe property that is used to do the initial hook-up. Usage looks like this: <UserControl … SizeObserver.Observe=”True” SizeObserver.ObservedWidth=”{Binding Width, Mode=OneWayToSource}” SizeObserver.ObservedHeight=”{Binding … Read more

How do I bind a WPF DataGrid to a variable number of columns?

Here’s a workaround for Binding Columns in the DataGrid. Since the Columns property is ReadOnly, like everyone noticed, I made an Attached Property called BindableColumns which updates the Columns in the DataGrid everytime the collection changes through the CollectionChanged event. If we have this Collection of DataGridColumn’s public ObservableCollection<DataGridColumn> ColumnCollection { get; private set; } … Read more

INotifyPropertyChanged vs. DependencyProperty in ViewModel

Kent wrote an interesting blog about this topic: View Models: POCOs versus DependencyObjects. Short summary: DependencyObjects are not marked as serializable The DependencyObject class overrides and seals the Equals() and GetHashCode() methods A DependencyObject has thread affinity – it can only be accessed on the thread on which it was created I prefer the POCO … Read more

Binding a WPF ComboBox to a custom list

You set the DisplayMemberPath and the SelectedValuePath to “Name”, so I assume that you have a class PhoneBookEntry with a public property Name. Have you set the DataContext to your ConnectionViewModel object? I copied you code and made some minor modifications, and it seems to work fine. I can set the viewmodels PhoneBookEnty property and … Read more

Binding to static property

If the binding needs to be two-way, you must supply a path. There’s a trick to do two-way binding on a static property, provided the class is not static : declare a dummy instance of the class in the resources, and use it as the source of the binding. <Window.Resources> <local:VersionManager x:Key=”versionManager”/> </Window.Resources> … <TextBox … Read more

How to bind RadioButtons to an enum?

You can further simplify the accepted answer. Instead of typing out the enums as strings in xaml and doing more work in your converter than needed, you can explicitly pass in the enum value instead of a string representation, and as CrimsonX commented, errors get thrown at compile time rather than runtime: ConverterParameter={x:Static local:YourEnumType.Enum1} <StackPanel> … Read more

How do I use WPF bindings with RelativeSource?

If you want to bind to another property on the object: {Binding Path=PathToProperty, RelativeSource={RelativeSource Self}} If you want to get a property on an ancestor: {Binding Path=PathToProperty, RelativeSource={RelativeSource AncestorType={x:Type typeOfAncestor}}} If you want to get a property on the templated parent (so you can do 2 way bindings in a ControlTemplate) {Binding Path=PathToProperty, RelativeSource={RelativeSource TemplatedParent}} … Read more