Tôi đang cố gắng liên kết thuộc tính IsSelected của một ListViewItem với thuộc tính trong một ViewModel. Nó hoạt động tốt trong WPF, nhưng trong Windows RT thuộc tính IsSelected không bao giờ được thiết lập.ListViewItem IsSelected Binding - Làm việc cho WPF, nhưng không phải cho WinRT
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:
<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>
Tôi có thể nhấp vào các mục trên màn hình, nhưng tài sản IsSelected không tuyên truyền đến ViewModel. Bất kỳ ý tưởng tại sao?
Ông có thể cung cấp một liên kết? Tôi không thực sự biết những gì để tìm kiếm. –
Xem câu trả lời của tôi cho câu hỏi này: http://stackoverflow.com/questions/11857505/how-do-i-do-bindings-in-itemcontainerstyle-in-winrt –
Tôi đang sử dụng phần mở rộng Bộ công cụ WinRT Xaml. Nó hoạt động đúng. Cảm ơn – vivekp