Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Private setter throwing error only on release build

Tags:

c#

mvvm

wpf

I have a map-tile setting I'm updating through a menu-button. I've got an odd situation where I was only hitting an error on release builds. Code is as follows:

View-Model

private KnownTileSource _selectedTile;
public KnownTileSource SelectedTile
{
    get { return _selectedTile; }
    private set
    {
        _selectedTile = value;
        ...
        OnPropertyChanged("SelectedTile");
    }
}

View

<Window ...
 xmlns:predefined="clr-namespace:BruTile.Predefined;assembly=BruTile">
...
    <MenuItem Header="_Bing Aerial" Command="{Binding ChangeTileCommand}" CommandParameter="{x:Static predefined:KnownTileSource.BingAerial}" IsChecked="{Binding Path=SelectedTile, Mode=TwoWay, Converter={local:EnumToBooleanConverter}, ConverterParameter=BingAerial}"/>
...
</Window>

This was all working fine in my developer environment but when I generated a release build I was getting the following:

Error

System.InvalidOperationException: A TwoWay or OneWay ToSource binding cannot work on the read-only property 'SelectedTile'...

Simple solution, change private set to set in the above SelectedTile property.

So how come this was not throwing the error during debug and only during release? I can't see how this was ever working during debug mode.

like image 834
Karl Morosiuk Avatar asked Oct 16 '17 12:10

Karl Morosiuk


1 Answers

This is a known bug that has been fixed: https://connect.microsoft.com/VisualStudio/feedback/details/773682/wpf-property-with-private-setter-is-updated-by-a-twoway-binding

So you may get this behaviour if your app targets .NET Framework 4.0 but .NET Framework 4.5+ is installed on your development machine.

You should remove the private keyword from the setter to fix the issue.

like image 153
mm8 Avatar answered Oct 06 '22 01:10

mm8