在Silverlight MVVMLight 4.0应用程序中,我有一个列表框,一个文本框和一个复选框。 列表框的ItemsSource绑定到viewmodel中的对象列表。 列表框的SelectedItem对视图模型中的对象(SelectedActivity)进行双向绑定。Silverlight MVVM - Twoway绑定未在Listbox上触发点击
文本框的Text和复选框的IsSelected属性都是双向绑定到ViewModel中的SelectedActivity对象(Name和Selected properties)。 没有隐藏代码。
这工作正常:更改文本框中的名称或选中/取消选中复选框,然后选项卡将更改该对象的基础属性。
但是,当我更改名称(或选中状态),然后立即单击列表中的另一个项目时,更改未注册。
有没有人有这方面的解决方法?
亲切的问候,
卡雷尔
这是XAML:
<ListBox Height="251" HorizontalAlignment="Left" Margin="11,39,0,0" Name="activitiesListBox" ItemsSource="{Binding Activities.Items}" VerticalAlignment="Top" Width="139"
SelectedItem="{Binding Activities.SelectedActivity, Mode=TwoWay}">
这是活动类保持绑定到列表中的项目:
public class CLJActivitiesViewModel : ViewModelBase
{
/// <summary>
/// Initializes a new instance of the ActivitiesViewModel class.
/// </summary>
public CLJActivitiesViewModel()
{
////if (IsInDesignMode)
////{
//// // Code runs in Blend --> create design time data.
////}
////else
////{
//// // Code runs "for real": Connect to service, etc...
////}
}
#region items
/// <summary>
/// The <see cref="Items" /> property's name.
/// </summary>
public const string ItemsPropertyName = "Items";
private ObservableCollection<CLJActivityViewModel> m_Items = null;
/// <summary>
/// Gets the Items property.
/// TODO Update documentation:
/// Changes to that property's value raise the PropertyChanged event.
/// This property's value is broadcasted by the Messenger's default instance when it changes.
/// </summary>
public ObservableCollection<CLJActivityViewModel> Items
{
get
{
return m_Items;
}
set
{
if (m_Items == value)
{
return;
}
var oldValue = m_Items;
m_Items = value;
RaisePropertyChanged(ItemsPropertyName, oldValue, value, true);
}
}
#endregion
#region SelectedActivity
/// <summary>
/// The <see cref="SelectedActivity" /> property's name.
/// </summary>
public const string SelectedActivityPropertyName = "SelectedActivity";
private CLJActivityViewModel m_SelectedActivity = null;
/// <summary>
/// Gets the SelectedActivity property.
/// TODO Update documentation:
/// Changes to that property's value raise the PropertyChanged event.
/// This property's value is broadcasted by the Messenger's default instance when it changes.
/// </summary>
public CLJActivityViewModel SelectedActivity
{
get
{
return m_SelectedActivity;
}
set
{
if (m_SelectedActivity == value)
{
return;
}
var oldValue = m_SelectedActivity;
m_SelectedActivity = value;
RaisePropertyChanged(SelectedActivityPropertyName, oldValue, value, true);
}
}
#endregion
public override void Cleanup()
{
// Clean own resources if needed
base.Cleanup();
}
}
这将有助于如果您发布至少是你的viewmodel/view代码的相关部分。您的列表是ObservableCollection,而您的视图模型是否实现INotifyPropertyChanged? – 2010-08-30 16:44:37
是的,列表绑定到一个ObservableCollection并且视图模型正在实现INotifyPropertyChanged。我使用了MVVMLight模板。 我会用相关的代码更新这个问题。 – Karel 2010-08-31 08:13:07