5

我正在尝试使用新的 x:Bind 绑定 ListView.SelectedItem。我的代码:

看法:

//MainPage.xaml:

<Page
x:Class="BrokenListSample.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:BrokenListSample"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<Grid Background="Beige">
    <Grid.RowDefinitions>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
    </Grid.RowDefinitions>

    <ListView Grid.Row="0" Background="LawnGreen" 
              ItemsSource="{x:Bind ViewModel.MyItems, Mode=OneWay}"
              SelectedItem="{x:Bind ViewModel.BestItem, Mode=TwoWay}"
              Width="300" Height="300"/>

    <ListView Grid.Row="1" Background="LawnGreen" 
              ItemsSource="{Binding MyItems, Mode=OneWay}"
              SelectedItem="{Binding BestItem, Mode=TwoWay}"
              Width="300" Height="300"/>
</Grid>

代码隐藏:

//MainPage.xaml.cs:

using Windows.UI.Xaml.Controls;

namespace BrokenListSample
{
    public sealed partial class MainPage : Page
    {
        public MainPageViewModel ViewModel { get; set; }

        public MainPage()
        {
            InitializeComponent();
            DataContextChanged += (s, e) => { ViewModel = DataContext as MainPageViewModel; };
            DataContext = new MainPageViewModel();
        }
    }
}

最后是 ViewModel:

//MainPageViewModel.cs:

using System.Collections.ObjectModel;
using System.ComponentModel;

namespace BrokenListSample
{
    public class MainPageViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(string name)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
        }

        private ObservableCollection<string> _myItems;
        public ObservableCollection<string> MyItems
        {
            get { return _myItems; }
            set { _myItems = value; OnPropertyChanged("MyItems"); }
        }

        private string _bestItem;
        public string BestItem
        {
            get { return _bestItem; }
            set { _bestItem = value; OnPropertyChanged("BestItem"); }
        }

        public MainPageViewModel()
        {
            MyItems = new ObservableCollection<string>() { "One", "Two", "Three", "Four" };
        }
    }
}

如您所见,我的 MainPage 上有两个 ListView 控件。如果您尝试运行此代码,请根据您要检查的绑定类型对其中一个进行注释。第二行的 ListView 使用旧的良好绑定,它只是简单地工作。这里没有惊喜。

Surprise 使用了导致 StackOverflowException 的新 x:Bind。在 OneWay 模式下工作正常 - 但是每当我单击其中一个项目时,TwoWay 都会抛出 StackOverflowException ......很搞笑......

我的问题很简单——“为什么以及如何解决这个问题?”

4

2 回答 2

4

我面临着同样的问题。查看堆栈跟踪,我发现我的 listview 正在修改 viewmodel,它正在提升 OnPropertyChanged,它修改了 listview ...要解决这个问题,您应该修改绑定属性的设置器:

public string BestItem
{
  get { return _bestItem; }
  set
  {
    if (_bestItem != value)
    {
      _bestItem = value;
      OnPropertyChanged(nameof(BestItem));
    }
  }
}
于 2016-04-27T12:34:24.960 回答
3

您的SelectedItemListView 来自类型Object,但您尝试引用它,String因此您需要一个简单的转换器。

我发现你可以只使用一个通用的而不做任何事情但实施IValueConverter- 这在这个答案中提到

public class GenericConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        return value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        // no convert to a specific type needed -> the "value" is already an instance of the correct type.
        return value;
    }
}

现在你只需要在你的 x:Bind 中引用这个转换器

<ListView ...
          ItemsSource="{x:Bind ViewModel.MyItems, Mode=OneWay}"
          SelectedItem="{x:Bind ViewModel.BestItem, Mode=TwoWay, 
          Converter={StaticResource GenericConverter}}"
          .../>

在您的 App.xaml 中(使此转换器可用于您的所有视图):

<Application
x:Class="Namespace.App"
...
xmlns:Converters="using:Namespace.Converters">
<Application.Resources>
    <ResourceDictionary>
        ...
        <Converters:GenericConverter x:Key="GenericConverter"/>
        ...
    </ResourceDictionary>
</Application.Resources>

于 2016-05-18T08:12:14.547 回答