本文介绍了ListViewItem的IsSelected绑定 - 工程为WPF,但不适合的WinRT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想一个ListViewItem的的IsSelected属性绑定到一个ViewModel的属性。它工作正常在WPF,但在Windows RT IsSelected属性永远不会得到设定。
I'm trying to bind the IsSelected property of a ListViewItem to a property in a ViewModel. It works fine in WPF, but in Windows RT the IsSelected property is never getting set.
public class Item : INotifyPropertyChanged
{
private readonly string name;
private bool isSelected;
public event PropertyChangedEventHandler PropertyChanged;
public bool IsSelected
{
get { return isSelected; }
set { isSelected = value; RaisePropertyChanged("IsSelected"); }
}
public string Name { get { return name; } }
public Item(string name)
{
this.name = name;
}
protected void RaisePropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
handler.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public class ViewModel
{
private readonly ObservableCollection<Item> items = new ObservableCollection<Item>(Enumerable.Range(0, 10).Select(p => new Item(p.ToString())));
public ObservableCollection<Item> Items { get { return items; } }
}
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
DataContext = new ViewModel();
}
}
XAML:
xaml:
<StackPanel Orientation="Horizontal">
<ListView ItemsSource="{Binding Path=Items}" SelectionMode="Multiple">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=TwoWay}"/>
</Style>
</ListView.ItemContainerStyle>
</ListView>
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Name}"/>
</DataTemplate>
</ListView.ItemTemplate>
</StackPanel>
我能点击屏幕上的项目,但IsSelected属性不传播到视图模型。任何想法,为什么?
I'm able to click on the items on the screen, but the IsSelected property isn't propagating to the ViewModel. Any ideas why?
推荐答案
的WinRT不支持绑定setter方法都与Windows 8.0。兵寻求解决方法。
WinRT doesn't support bindings in setters at all as of Windows 8.0. Bing for workarounds.
这篇关于ListViewItem的IsSelected绑定 - 工程为WPF,但不适合的WinRT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!