我有2 ComboBoxes
的视图。当在第一个项目中选择一个项目时,将获得第二个项目的列表。链接视图和视图模型的属性如下:
在视图后面的代码中,我链接了视图和视图模型
var viewModel = new MetrologyFileViewModel();
DataContext = viewModel;
WPF:
<ComboBox ItemsSource="{Binding MetrologyProperties}"
SelectedIndex="{Binding NewSelectedMetrologyPropertyIndex}"
Grid.Column="1"
Grid.ColumnSpan="2"
Style="{StaticResource ComboBoxStyle1}" />
<ComboBox ItemsSource="{Binding NewMetrologyData}"
SelectedIndex="{Binding NewSelectedMetrologyDataIndex}"
Grid.Row="1"
Grid.Column="1"
Grid.ColumnSpan="2"
Style="{StaticResource ComboBoxStyle1}" />
C#:
/// <summary>
/// The new metrology property of the metrology data.
/// </summary>
public int NewSelectedMetrologyPropertyIndex
{
get { return _newSelectedMetrologyPropertyIndex; }
set
{
_newSelectedMetrologyPropertyIndex = value;
_newMetrologyData = _newSelectedMetrologyPropertyIndex > 0 ? new ObservableCollection<MetrologyData> { new MetrologyData() }.AddRange(DbServiceMetrologyData.GetOnProperty(MetrologyProperties[_newSelectedMetrologyPropertyIndex])) : null;
NotifyPropertyChanged();
NotifyPropertyChanged(nameof(NewMetrologyData));
}
}
/// <summary>
/// The new available metrology data that can be chosen from.
/// </summary>
public ObservableCollection<MetrologyData> NewMetrologyData
{
get { return _newMetrologyData; }
set
{
_newMetrologyData = value;
NotifyPropertyChanged();
}
}
/// <summary>
/// The new metrology data of the metrology fie.
/// </summary>
public int NewSelectedMetrologyDataIndex
{
get { return _newSelectedMetrologyDataIndex; }
set
{
_newSelectedMetrologyDataIndex = value;
NotifyPropertyChanged();
}
}
AddRange
方法:
/// <summary>
/// Add a range of items to a <see cref="ObservableCollection{T}"/>.
/// </summary>
/// <typeparam name="T">The type of the <see cref="ObservableCollection{T}"/></typeparam>
/// <param name="sourceCollection">The <see cref="ObservableCollection{T}"/> where the items have to be added to.</param>
/// <param name="newCollection"><see cref="ObservableCollection{T}"/> with the new items.</param>
public static ObservableCollection<T> AddRange<T>(this ObservableCollection<T> sourceCollection, ObservableCollection<T> newCollection)
{
if (newCollection != null)
foreach (var item in newCollection)
sourceCollection.Add(item);
return sourceCollection;
}
我第一次在第一次ComboBox
中选择一个属性时,第二次更新很好,虽然视图(我认为)没有完全更新,因为列表应该显示的空间非常小(见图)。
只要我没有点击第二个ComboBox
,这就会继续有效。从我打开(点击)第二个ComboBox
的那一刻起,当我在第一个中选择一个属性时,它就会停止更新。
更新
我发现只要拨打DbServiceMetrologyData.GetOnProperty(MetrologyProperties[_newSelectedMetrologyPropertyIndex])
问题就解决了,我就不知道为什么会这样。
答案 0 :(得分:0)
我发现只要调用DbServiceMetrologyData.GetOnProperty(MetrologyProperties[_newSelectedMetrologyPropertyIndex])
问题就解决了,我不知道为什么会这样。