【发布时间】:2018-10-21 15:51:24
【问题描述】:
我有一个 ListView,我想更改 ViewModel 中的 SelectionMode。这工作了一半。它的工作原理是我单击一个按钮,并且 SelectionMode 在 Single 和 Multiple 之间变化。但是,如果我在 ListView 中有一个选定的项目并更改为 Multiple,那么我在 MainPage.g.cs 中的此时会得到 StackOverflowException:
public static void Set_Windows_UI_Xaml_Controls_ListViewBase_SelectionMode(global::Windows.UI.Xaml.Controls.ListViewBase obj, global::Windows.UI.Xaml.Controls.ListViewSelectionMode value)
{
obj.SelectionMode = value; //<--- here is the error
}
这是我的列表视图:
<ListView x:Name="MyList" Grid.Row="2" ItemsSource="{x:Bind ViewModel.Threads, Mode=OneWay}" SelectedItem="{x:Bind ViewModel.Selected, Mode=TwoWay}" ShowsScrollingPlaceholders="True" SelectionMode="{x:Bind ViewModel.ListSelectionMode, Mode=OneWay}">
<interactivity:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="SelectionChanged">
<core:InvokeCommandAction Command="{x:Bind ViewModel.SelectionChangedCommand}"
InputConverter="{StaticResource SelectionChangedConverter}"
InputConverterParameter="{Binding ElementName=MyList}" />
</core:EventTriggerBehavior>
</interactivity:Interaction.Behaviors>
</ListView>
这里是 ViewModel:
private ListViewSelectionMode _listSelectionMode = ListViewSelectionMode.Single;
public ListViewSelectionMode ListSelectionMode
{
get { return _listSelectionMode; }
set
{
if (_listSelectionMode != value)
{
_listSelectionMode = value;
RaisePropertyChanged();
}
}
}
private List<object> _selectedThreads = new List<object>();
public List<object> SelectedThreads
{
get { return _selectedThreads; }
set
{
if (value != _selectedThreads)
{
_selectedThreads = value;
RaisePropertyChanged();
}
}
}
private string _selectedThreadCounter;
public string SelectedThreadCounter
{
get { return _selectedThreadCounter; }
set
{
if (value != _selectedThreadCounter)
{
_selectedThreadCounter = value;
RaisePropertyChanged();
}
}
}
private RelayCommand<IList<object>> _selectionChangedCommand;
public RelayCommand<IList<object>> SelectionChangedCommand
{
get
{
if (_selectionChangedCommand == null && ListSelectionMode == ListViewSelectionMode.Multiple)
{
_selectionChangedCommand = new RelayCommand<IList<object>>(
items =>
{
SelectedThreads = items.ToList();
SelectedThreadCounter = "(" + SelectedThreads.Count + ")";
}
);
}
return _selectionChangedCommand;
}
}
这里是 SelectionChangedConverter:
public class SelectionChangedConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
var listView = parameter as ListView;
return listView.SelectedItems;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
我希望这些代码足以帮助您找到答案。
更新: 也许我应该在 ViewModel 中添加我的 ItemsSource 看起来像这样:
private IRealmCollection<ThreadRealm> _threads;
public IRealmCollection<ThreadRealm> Threads
{
get { return _threads; }
set
{
if (_threads != value)
{
_threads = value;
RaisePropertyChanged();
}
}
}
它是来自Realm 数据库的 IRealmCollection
【问题讨论】:
-
我在
RaisePropertyChanged的Selected属性中获得了StackoOverflowException。 -
当我将 x:Bind 更改为 Binding 时,RaisePropertyChanged 也会出现异常。但为什么?或者我能做些什么来解决这个问题?
标签: listview mvvm uwp stack-overflow