【问题标题】:ListViewItem IsSelected Binding - Works for WPF, but not for WinRTListViewItem IsSelected 绑定 - 适用于 WPF,但不适用于 WinRT
【发布时间】:2013-04-13 23:22: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。任何想法为什么?

【问题讨论】:

    标签: c# xaml windows-runtime winrt-xaml


    【解决方案1】:

    从 Windows 8.0 开始,WinRT 根本不支持 setter 中的绑定。必应寻找解决方法。

    【讨论】:

    • 你能提供一个链接吗?我真的不知道要搜索什么。
    • 查看我对这个问题的回答:stackoverflow.com/questions/11857505/…
    • 我正在使用 WinRT Xaml 工具包扩展。它工作正常。谢谢
    【解决方案2】:

    实现这一点的一个好方法是继承 ListView

    public class MyListView : ListView
        {
            protected override void PrepareContainerForItemOverride(Windows.UI.Xaml.DependencyObject element, object item)
            {
                base.PrepareContainerForItemOverride(element, item);
                // ...
                ListViewItem listItem = element as ListViewItem;
                Binding binding = new Binding();
                binding.Mode = BindingMode.TwoWay;
                binding.Source = item;
                binding.Path = new PropertyPath("Selected");
                listItem.SetBinding(ListViewItem.IsSelectedProperty, binding);
            }
        }
    

    或者,您似乎也可以使用WinRT XAML Toolkit

    <ListView
                x:Name="lv"
                Grid.Row="1"
                Grid.Column="1"
                SelectionMode="Multiple"
                HorizontalAlignment="Left"
                Width="500">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <TextBlock
                            Extensions:ListViewItemExtensions.IsSelected="{Binding IsSelected}"
                            Extensions:ListViewItemExtensions.IsEnabled="{Binding IsEnabled}"
                            Text="{Binding Text}"
                            Margin="15,5"
                            FontSize="36" />
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
    

    就我个人而言,我使用了第一种方法,因为它更灵活,而且我需要绑定一些自动化属性。

    感谢 ForInfo 和 ehuna: http://social.msdn.microsoft.com/Forums/windowsapps/en-US/9a0adf35-fdad-4419-9a34-a9dac052a2e3/listviewitemisselected-data-binding-in-style-setter-is-not-working

    【讨论】:

    • 我正在使用 WinRT Xaml 工具包扩展。它工作正常。谢谢
    • Xaml 工具包似乎只对我起作用,即使 Mode=TwoWay 也是如此。如果我在 ListView 中选择某些内容,它不会更新 ViewModel?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-19
    • 2016-01-27
    • 1970-01-01
    • 1970-01-01
    • 2018-08-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多