【发布时间】:2016-05-03 21:08:42
【问题描述】:
我在一个 MVVM/PRISM 应用程序的 WPF 中有一个列表视图,它可能包含一对多的元素。当列表视图仅包含 1 个元素并且我选择它时,即使我将 SelectedIndedx 值设置为 -1,我也无法随后重新选择它。更糟糕的是,如果我让应用程序使用不同的单个元素更新列表视图,我也无法选择该元素。当它是列表视图中的唯一项目时,我可以实现对项目的选择的唯一方法是让应用程序显示多个项目并选择第一个以外的其他项目。然后,当我让应用程序显示一个包含单个项目的列表视图时,我可以再次选择它 - 但只能选择一次。
在我无法在列表视图中选择单个项目的情况下,服务例程永远不会触发。
我尝试使用“Listview.Container.Style”和 IsSelected 属性来实现我在此处找到的 XAML 建议,但这不起作用。
我的列表视图相当简单:
<ListView Name="lstEditInstance"
Grid.Row="5"
ItemsSource="{Binding Path=InstanceList,Mode=TwoWay}"
Width="488"
FontFamily="Arial" FontSize="11"
HorizontalAlignment="Left" VerticalAlignment="Stretch"
Margin="10,96,0,28"
SelectedIndex="{Binding Path=InstanceSelectedIndex}">
</ListView>
服务程序是:
private void OnInstanceSelectedIndexChanged()
{
// Handle case where user hits Enter without making a selection:
if (_instanceIndex == -1) return;
// Get the instance record for the row the user clicked on as a
// ResourceInstance class named "InstanceRecord".
InstanceRecord = _instanceList[_instanceIndex];
_instanceNumber = InstanceRecord.Instance;
FormInstName = InstanceRecord.InstName;
FormInstEnabled = InstanceRecord.Enabled;
FormInstState = InstanceRecord.InitialState;
FormInstIPAddress = InstanceRecord.IPAddress;
FormInstPort = InstanceRecord.Port.ToString();
FormInstSelectedURL = InstanceRecord.UrlHandler;
} // End of "OnResourceSelectedIndexChanged" method.
“InstanceList”是一个可观察的集合。
我会很感激一些建议。提前感谢您的帮助。
【问题讨论】: