【问题标题】:Binding ListView ItemTemplate to Key and Value of Dictionary<string,string>将 ListView ItemTemplate 绑定到 Dictionary<string,string> 的键和值
【发布时间】:2013-02-27 11:24:29
【问题描述】:

我有一个Dictionary&lt;string,string&gt;,其中包含一些我想在ListView 中显示的数据。 ListView 绝对“看到”正确数量的项目(因为我从第二个 TextBlock 中看到了适当数量的冒号),但它既不显示 Key 也不显示 Value 绑定。

我需要Converter 还是什么?

我是否需要更改并改用ObservableCollection&lt;KeyValuePair&lt;string,string&gt;&gt;

<ListView Grid.Row="1" IsItemClickEnabled="False" 
          SelectionMode="None" IsHitTestVisible="False"
          IsSwipeEnabled="False"
          ItemsSource="{Binding SymbolInfoText}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Key}"/>
                <TextBlock Text=": "/>
                <TextBlock Text="{Binding Value}"/>
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

public Dictionary<string, string> SymbolInfoText
{
    get
    {
        if (selectedItem == null)
        {
            return new Dictionary<string, string> { { "Error", "No Item Selected" } };
        }
        return selectedItem.GenerateObjectProperties(CurrentLocation, LocalMapManager.Map.Heading);
    }
}

【问题讨论】:

  • 我在我们的代码中找不到错误。根据您的描述,这不是绑定问题。这与Dictionary 中的数据有关。请使用您的 .cs 文件的完整代码更新您的帖子。

标签: c# data-binding windows-store-apps winrt-xaml windows-store


【解决方案1】:

如果您在事后添加项目(在启动应用程序并建立绑定链接之后),那么Dictionary&lt;string,string&gt; 将不是最佳选择,因为它没有实现INotifyCollectionChanged。尝试在将元素添加到字典后立即设置ItemsSource,您会看到项目实际上出现在列表中,您只需更新绑定即可。

所以是的,ObservableCollection&lt;T&gt; 将是一个更优化的解决方案,因为它会在核心集合发生更改时发送通知。

【讨论】:

  • 我在 SymbolInfoText 属性上手动触发了 OnPropertyChanged,这就是我感到困惑的原因。我在问题中为该属性添加了一些额外的逻辑;当一个项目被选中(因此 selectedItem 不再为空)时,调用 OnPropertyChanged 并更改字典(以便出现三个冒号而不仅仅是一个冒号)。由此看来,在我看来绑定是存在的,但是字符串周围存在某种类型错误...?
  • 查看您的代码,您尝试解决问题的方式存在固有问题。为什么要检查属性 getter 中的选定项目?还有,什么是selectedItem?
  • 可观察字典的好资源:drwpf.com/blog/2007/09/16/… ... 如果 OP 坚持使用某种字典。
  • ListView 正在显示有关selectedItem 的信息。 When the selection changes, the newly selected item is queried for new information (most of which has changed since it was last selected (if ever), hence the newly generated dictionary each time). SelectedItem 是一个 MapPoint,它是一个特定于应用程序的类。
  • 我也尝试使用ObservableCollection&lt;KeyValuePair&lt;string,string&gt;&gt; 并遇到了同样的问题。我很确定集合正在正确更新 - 调试器上的断点显示每次 selectedItem 更改时都会调用 getter,正如我所料。它还返回我期待的Dictionary,这反过来又添加了ListViewItems。除了静态指定的文本(如果我删除IsHitTestVisible="False",它不会显示正确数量的命中测试)。
【解决方案2】:

试试这个,它会告诉你你想要什么,但你使用它的方式不是在 WPF 中使用集合的正常方式

public partial class MainWindow : Window
{
    public MainWindow()
    {
        this.DataContext = this;
        InitializeComponent();

    }


    public Dictionary<string, string> SymbolInfoText
    {
        get
        {
            return new Dictionary<string, string> { { "Error", "No Item Selected" }, { "Error1", "No Item Selected" } };


        }
    }


}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-04
    • 1970-01-01
    • 1970-01-01
    • 2011-06-15
    • 2011-07-27
    相关资源
    最近更新 更多