开发者

Listbox Binded to ObservableCollection strange issue MVVM WPF

开发者 https://www.devze.com 2023-04-10 16:21 出处:网络
this is my first question posted here... I have an user interface that contains a ListBox with pre loaded items and the GUI allows the user to add or remove items to that listbox. Very Simple.

this is my first question posted here... I have an user interface that contains a ListBox with pre loaded items and the GUI allows the user to add or remove items to that listbox. Very Simple. The problem is that when I select items in the ListBox, I cannot "deselect" them if they where added to the ObservableCollection "in runtime" (The items loaded "by default" works fine!) I have a ListBox binded to an ObservableCollection like this:

<ListBox ItemsSource="{Binding RolFunciones}" 
SelectionMode="Single"
DisplayMemberPath="Descripcion" 
SelectedItem="{Binding SelectedRolFunciones, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Width="600"
Height="300" />    

In my ViewModel I have:

ObservableCollection<RolFuncion> mRolFunciones;
public ObservableCollection<RolFuncion> RolFunciones 
{
    get { return mRolFunciones; }
    set { mRolFunciones = value; OnPropertyChanged(() => RolFunciones); }
}

RolFuncion mSelectedRolFunciones;
public RolFuncion SelectedRolFunciones 
{
    get { return mSelectedRolFunciones; }
    set { mSelectedRolFunciones = value; OnPropertyChanged(() => SelectedRolFunciones); }
}

...

// Constructor
public RolViewModel()
{
    RolFunciones = new ObservableCollection<RolFuncion>();
    RolFunciones.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(RolFunciones_CollectionChanged);
}

void RolFunciones_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
    OnPropertyChanged(() => RolFunciones);
}

... And I have the method that Adds an Item to the ObservableCollection like this

void AddFunctionExecute()
{
    RolFunciones.Add(new RolFuncion() { Descripcion = "dummy" });
开发者_高级运维}

Does somebody had the same issue?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号