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?
In addition to Phil’s excellent suggestions, you can also use the UsedImplicitlyAttribute. You can use Nuget to do add the dll for you
And then R# will itself offer to decorate the setter with the attribute:
I find it less noisy than a comment, and well suited to a ViewModel where it’s understood that data bindings are typically unknown to R#. (sometimes I actually prefer a comment to remind me why I wanted R# to shut up, but not in this case).
Cheers,
Berryl