【问题标题】:ListView SelectedItem not firing in MVVM app using Template 10 UWP使用模板 10 UWP 在 MVVM 应用程序中未触发 ListView SelectedItem
【发布时间】:2016-04-13 10:44:36
【问题描述】:

我有这个 XAML...

<ListView ItemsSource="{Binding AllRoundIDs}" Height="96"
          SelectedItem="{Binding AllRoundsSelectedItem, Mode=TwoWay}"
          SelectionMode="Single"
          >
    <ListView.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Key}" FontSize="30" Foreground="White" Padding="0,0,0,1" />
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

我的 ViewModel 中的这段代码...

    private ObservableCollection<KeyValuePairs> _AllRoundIDs;
    public ObservableCollection<KeyValuePairs> AllRoundIDs
    {
        get { return _AllRoundIDs; }
        private set { Set(ref _AllRoundIDs, value); }
    }
    private KeyValuePairs _AllRoundsSelectedItem;
    public KeyValuePairs AllRoundsSelectedItem
    {
        get { return _AllRoundsSelectedItem; }
        private set { Set(ref _AllRoundsSelectedItem, value); }
    }

KeyValuePairs 类看起来像这样...

public class KeyValuePairs
{
    public string Key { get; set; }
    public string Value { get; set; }
}

当我运行我的应用程序时,我会按预期在我的 ListView 中获得项目。所以我知道数据绑定正在工作。

当我点击一个项目时,我没有得到任何生命。与 AllRoundsSelectedItem 的绑定不会触发。此代码在 WPF 应用程序中运行良好,但在 UWP 中我一无所获。我错过了什么?

提前致谢。

【问题讨论】:

    标签: c# xaml listview mvvm uwp


    【解决方案1】:

    您的AllRoundsSelectedItem 属性的设置器是私有的,因此ListView 无法访问它。您可能会发现您的输出中存在与此相关的绑定错误。

    顺便说一句,因为KeyValuePairs 类代表一对,它可能应该命名为KeyValuePair

    【讨论】:

    • 天啊,我不敢相信我没有看到。你的新鲜眼睛。谢谢。
    • 另外,我确实使用了 KeyValuePair 中的构建,但没有取得任何进展,所以我想尝试我自己的课程。非常感谢。
    猜你喜欢
    • 2017-05-15
    • 1970-01-01
    • 2017-02-24
    • 1970-01-01
    • 2019-04-30
    • 2020-11-03
    • 2016-06-18
    • 1970-01-01
    • 2018-05-30
    相关资源
    最近更新 更多