我有一个连接到我的viewmodel的视图。每当用户从视图中更改selecteditem时,我会获取附加数据以附加到所选项目。如果我在更新selecteditem后没有提升属性本身,则结果将不会在UI上更新。这显然不是预期的实现,但也许我不清楚如何通知属性。
这是绑定到多个控件的支持属性。
public Break SelectedData
{
get { return _selectedData; }
set
{
if (_selectedData!= value && value != null)
{
_selectedData= value;
DeviceName = value.Feeder;
OnPropertyChanged("SelectedData");
}
}
}
这是将交互触发器绑定到viewmodel中的异步命令的组合框:
<ComboBox ItemsSource="{Binding Feeders}" DisplayMemberPath="OMSName" SelectedItem="{Binding SelectedData, Mode=TwoWay}" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding SelectionChangedCommand}" CommandParameter="{Binding SelectedItem}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ComboBox>
在viewmodel中,selectionchanged命令执行我的异步函数,以便在设置selectionchanged命令时添加其他数据:
SelectionChangedCommand = new RelayAsyncCommand(UpdateSelectedData); --In the constructor of the viewmodel
public async void UpdateSelectedData()
{
try
{
await _breakRepo.GetEventData(SelectedData);
if (SelectedData.AttachedEvent.IsOpen)
{
SelectedData.Attributes= new EventAttributes
{
TotalAffected = SelectedData.AttachedEvent.Attributes.TotalAffected,
TotalRestored = SelectedData.AttachedEvent.Attributes.TotalRestored,
TotalOut = SelectedData.AttachedEvent.Attributes.TotalOut,
TotalCritical = SelectedData.AttachedEvent.Attributes.TotalCritical,
TotalMajor = SelectedData.AttachedEvent.Attributes.TotalMajor
};
await _breakRepo.CallEventsAsync(SelectedData);
}
else
{
await _breakRepo.GetBreakByFeederAsync(SelectedData);
}
}
catch (Exception ex)
{
System.Windows.MessageBox.Show(ex.Message);
}
finally
{
//IF I ADD ONPROPERTYCHANGED("SelectedData") the other fields that bind
//to the values will update as expected
CurrentlySearching = true;
}
}
答案 0 :(得分:3)
由于您未重新分配SelectedData
,控件将永远不会知道任何更改。
这就是你自己手动举起活动的原因
如果你绑定到某个地方,例如。 SelectedData.Attributes
然后您可能无法正确地在INotifyPropertyChanged
上实施SelectedData
。