这个 setter 最终如何工作?(列表视图多选) [英] How does this setter end up working? (ListView MultiSelect)

查看:26
本文介绍了这个 setter 最终如何工作?(列表视图多选)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ViewModel,我将它用作 ListViewItemsSource,它实现了一个名为 ISelectable 的接口:

I have a ViewModel which I'm using as an ItemsSource for a ListView, that implements an interface called ISelectable:

/// <summary>
/// An interface that should be implemented by a ViewModel that can be 
/// marked as selected, when multiple selections are allowed.
/// </summary>
public interface ISelectable
{
    /// <summary>
    /// Gets or sets a value indicating whether this instance is selected.
    /// </summary>
    /// <value>
    /// <c>true</c> if this instance is selected; otherwise, <c>false</c>.
    /// </value>
    bool IsSelected { get; set; }
}

ListView 显示搜索客户端"功能的搜索结果,因此所有项目都是 ClientViewModel 实例 - 而 ClientViewModel 实现了 ISelectable 所以有一个 IsSelected 属性:

The ListView is showing search results of a "search clients" feature, so all items are ClientViewModel instances - and ClientViewModel implements ISelectable so has a IsSelected property:

<ListView x:Name="SearchResultsList" ItemsSource="{Binding SearchResults}">
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="IsSelected" Value="{Binding IsSelected}" />
        </Style>
    </ListView.ItemContainerStyle>
    <ListView.ItemTemplate>
        <DataTemplate DataType="{x:Type viewModels:ClientViewModel}">
            <Label Content="{Binding Name}" />
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

这很好用;在窗口的 ViewModel 中,我可以定义这样的属性:

This works perfectly; in the window's ViewModel I can define a property like this:

public IEnumerable<ClientViewModel> SelectedClients 
{ 
    get 
    { 
        return _searchResults == null 
                              ? null 
                              : _searchResults.Where(e => e.IsSelected); 
    } 
}

我得到了我的期望.

我的问题是关于 XAML 的以下部分 - 设计者在 Value="{Binding}" 部分强调 IsSelected 并说无法解析属性'IsSelected' 在类型为 [窗口的 ViewModel 类型] 的数据上下文中":

The question I have is about the below part of the XAML - the designer underlines IsSelected in the Value="{Binding}" part and says "Cannot resolve property 'IsSelected' in data context of type [type of the window's ViewModel]":

    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="IsSelected" Value="{Binding IsSelected}" />
        </Style>
    </ListView.ItemContainerStyle>

  • 如何告诉设计者 ListView.ItemContainerStyle 的数据上下文应该与数据模板的数据上下文相同?
  • 如果 XAML 设计者说它无法解决,它如何在运行时工作?
    • How can I tell the designer that the data context for ListView.ItemContainerStyle should be the same as that of the data template?
    • How does it end up working at run-time, if the XAML designer says it can't be resolved?
    • 更新

      这是一个 ReSharper 警告.我可以关闭它,但我想知道的是setter 最终如何工作,因为我为此得到了正确的自动完成:

      This is a ReSharper warning. I could switch it off, but what I want to know is how does the setter end up working, because I get correct auto-complete for this:

          <ListView.ItemTemplate>
              <DataTemplate DataType="{x:Type viewModels:ClientViewModel}">
                  <Label Content="{Binding Name}" /> <!-- "Name" is available from IntelliSense -->
              </DataTemplate>
          </ListView.ItemTemplate>
      

      但不是为了那个:

          <ListView.ItemContainerStyle>
              <Style TargetType="ListViewItem">
                  <Setter Property="IsSelected" Value="{Binding IsSelected}" /> 
                  <!-- "IsSelected" isn't, IntelliSense is showing the members of the Window's ViewModel -->
              </Style>
          </ListView.ItemContainerStyle>
      

      推荐答案

      这应该是 Resharper 做的伎俩,因为我从未见过默认设计器智能感知在 Binding experssion 中指出此类错误.您可以完全切换 Resharper 以完全证明它.

      It should be the Resharper doing the trick as i never saw default designer intellisense pointing this type of error in Binding experssion. You can switch of the Resharper completly to proof it completly.

      由于您在 ListViewItem 样式中设置属性并且您使用的是 {Binding IsSelected},它会在每个 listviewitem 的 DataContext 中搜索 IsSelected,这是您的 ClientViewModel 并且具有 IsSelected 属性...因此绑定是完美的..设计师不够聪明,无法探测这么深

      since you are setting property inside ListViewItem style and you are using {Binding IsSelected}, it will search IsSelected in the DataContext of each listviewitem which is your ClientViewModel and that has IsSelected property...hence binding is perfect.. the designer is not smart enough to prob this deep

      这篇关于这个 setter 最终如何工作?(列表视图多选)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆