10

ListViewItem の IsSelected プロパティを ViewModel のプロパティにバインドしようとしています。WPF では問題なく動作しますが、Windows RT では IsSelected プロパティが設定されません。

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>

画面上の項目をクリックすることはできますが、IsSelected プロパティが ViewModel に反映されません。理由はありますか?

4

2 に答える 2

3

Windows 8.0 の時点で、WinRT はセッターでのバインドをまったくサポートしていません。Bing で回避策を探します。

于 2013-04-13T23:30:26.290 に答える