djangojazz
djangojazz

Reputation: 13232

Cannot bind a SelectedValue to a combobox for WPF in xaml from property

I am not understanding the binding principle error I am getting so I was curious: 'A TwoWay or OneWayToSource binding cannot work on the read-only property 'CurrentUser' of type 'Demo.ViewModel.MainWindowViewModel'.' My xaml is binding correctly EXCEPT for selected a default value for the combobox, 'SelectedValue'. Now this attribute will be fine if I do it manually with a 'SelectedValue = "1"' but not with code. The end goal is I want to generate a list of people and their identity seed from a database and this works fine. But I also want to use the Windows login to then set an auto default for the user. This would work if the property would work but I am guessing there is more I need to know on binding rules. Sort of like the WPF bindings only work with certain types and rules. I could trick it and make the 'Person' class have the defaultuser and then reference that but it seems that it should be it's own property to be well defined and I was hoping someone that was better at WPF binding would know my issue.

XAML:

<ComboBox Height="30" Width="170" Margin="10" x:Name="combopersons" 
                    FontSize="20"
                    ItemsSource="{Binding Path=People}"
                    DisplayMemberPath="FirstName"
                    SelectedValuePath="PersonId" 
                    SelectedValue="{Binding Path=CurrentUser}" />

partial C# code behind for viewmodel code:

ReadOnlyCollection<Person> _people;
string _curuser;

public string CurrentUser 
        { 
            get
            {
                if (_curuser == null)
                {
                    _curuser = "1";
                }
                return _curuser;
            } 

        }

public ReadOnlyCollection<Person> People 
        {
            get
            {
                if(_people == null)
                {
                    List<Person> persns = this.GetPeople();
                    _people = new ReadOnlyCollection<Person>(persns);
                }
                return _people;
            }
        }

        List<Person> GetPeople()
        {
            ExpensesEntities ee = new ExpensesEntities();
            return ee.tePersons.Select(x => new Person
                                         {
                                             PersonId = x.PersonID,
                                             FirstName = x.FirstName
                                         }).ToList();
        }

Upvotes: 0

Views: 1544

Answers (2)

Geerten
Geerten

Reputation: 1057

The selected value is a two way binding, meaning that the bound property is read to change the selected value, but if the selection is changed because the user uses the combobox, the property is set to that value.

So there are two solutions:

  • Make the binding one way: SelectedValue="{Binding Path=CurrentUser, Mode=OneWay}"
  • Create a setter on the read only property, and properly handle the value changes of the user.

The right choice depends on your application.

Upvotes: 2

zzfima
zzfima

Reputation: 1565

Maybe you forget define set method in CurrentUser property? Other thing - your class shall implemented IPropertyNotify interface, but it is not cause for the error.

Upvotes: 0

Related Questions