I use C#, MVVM, WPF and Resharper.
When using the following code:
public bool CombiBanksSelected
{
get { return _selectedBanksType == ESelectedBanksType.CombiBanks; }
set
{
I get a warning of Resharper: Make set accessor private.
When making the set method private, I get an InvalidOperationException: A TwoWay or OneWayToSource binding cannot work on the read-only property ''CombiBanksSelected'' of type ''PcgTools.ViewModels.PcgViewModel.'
Of course I can suppress it by adding:
public bool CombiBanksSelected
{
get { return _selectedBanksType == ESelectedBanksType.CombiBanks; }
// ReSharper disable MemberCanBePrivate.Global
set
// ReSharper restore MemberCanBePrivate.Global
{
But this is not looking nice and not feeling good. Is there a better alternative or solution for this problem?
According to the answer I should change the XAML code. Mine is:
<UserControl x:Class="PcgTools.PcgWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:ViewModels="clr-namespace:PcgTools.ViewModels" Height="Auto" Width="Auto"
Loaded="Window_Loaded">
<Grid>
...
<RadioButton Content="_Programs" Height="16" HorizontalAlignment="Left" Margin="12,12,0,0" Name="radioButtonPrograms" VerticalAlignment="Top"
IsChecked="{Binding Path=ProgramBanksSelected}" IsEnabled="{Binding Path=ProgramsEnabled}"/>
<RadioButton Content="_Combis" Height="16" HorizontalAlignment="Left" Margin="85,12,0,0" Name="radioButtonCombis" VerticalAlignment="Top"
IsChecked="{Binding Path=CombiBanksSelected}" IsEnabled="{Binding Path=CombisEnabled}"/>
I have the Resharper problem for both (and more) properties binded to IsChecked (ProgramBanksSelected and CombiBanksSelected in the code above).
In the answer is shown that I should use a DataTemplate, but I still can't figure out how exactly (with not using MVVM light's Locator).
How should I use the data context/template?