WPF + MVVM + RadioButton : How to handle binding with single property?

You need a IValueConverter. //define this in the Window’s Resources section or something similiarly suitable <local:GenderConverter x:Key=”genderConverterKey” /> <RadioButton Content=”M” IsChecked=”{Binding Gender, Converter={StaticResource ResourceKey=genderConverterKey}, ConverterParameter=M}” /> <RadioButton Content=”F” IsChecked=”{Binding Gender, Converter={StaticResource ResourceKey=genderConverterKey}, ConverterParameter=F}” /> The converter public class GenderConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { return ((string)parameter … Read more

WPF MVVM: how to bind GridViewColumn to ViewModel-Collection?

The Columns property is not a dependency property, so you can’t bind it. However, it might be possible to create an attached property that you could bind to a collection in your ViewModel. This attached property would then create the columns for you. UPDATE OK, here’s a basic implementation… Attached properties using System.Collections.Generic; using System.Collections.Specialized; … 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?.

MenuItem style with icon creates only one icon

Add x:Shared=false for your Icon value. To do that you should declare Image in resources: <Grid> <Grid.Resources> <Image x:Key=”imgCTX” x:Shared=”false” Source=”{Binding Path=Icon}” Height=”16px” Width=”16px”/> <HierarchicalDataTemplate DataType=”{x:Type ViewModels:HeaderedItemViewModel}” ItemsSource=”{Binding Path=Children}”> <ContentPresenter RecognizesAccessKey=”True”></ContentPresenter> </HierarchicalDataTemplate> <Style TargetType=”{x:Type MenuItem}”> <Setter Property=”Header” Value=”{Binding Path=Header}” /> <Setter Property=”InputGestureText” Value=”{Binding Path=InputGestureText}” /> <Setter Property=”Command” Value=”{Binding Path=Command}” /> <Setter Property=”Icon” Value=”{StaticResource imgCTX}” /> … Read more

Limit binding updates per second

A better approach would to remove the calls to NotifyPropertyChanged whenever the data changes. Create a timer and refresh on the timer. That way you can control the refresh rate, and it is not bound to the rate at which the data arrives.

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

What exactly are “WPF services”?

Martin Fowler has a description of what a service is in his Dependency Injection article. Put simply, a service is an object that provides functionality to be used by other objects. You’ll find the term used heavily when discussing the patterns Inversion of Control and Service Locator. To make this concrete with the topic at … Read more

typesafe NotifyPropertyChanged using linq expressions

What does the code that raises this look like? I’m guessing it is something like: NotifyOfPropertyChange(() => SomeVal); which is implicitly: NotifyOfPropertyChange(() => this.SomeVal); which does a capture of this, and pretty-much means that the expression tree must be constructed (with Expression.Constant) from scratch each time. And then you parse it each time. So the … Read more