在KeyValuePair上的ItemTemplate中绑定不起作用

时间:2012-03-21 12:38:58

标签: c# binding microsoft-metro .net-4.5

我遇到了一个问题,即在ListView的ItemTemplate中进行绑定。我的绑定目标是KeyValuePair。遵守守则:

XAML:

<ListView x:Name="listViewDataItems" CanDragItems="True" DragItemsStarting="event" SelectionMode="Multiple">
  <ListView.ItemTemplate>
    <DataTemplate>
      <StackPanel Orientation="Horizontal">
        <TextBlock Text="{Binding Path=Key}"/>
        <TextBlock Text=":"/>
        <TextBlock Text="{Binding Path=Value}"/>
      </StackPanel>
    </DataTemplate>
  </ListView.ItemTemplate>
</ListView>

C#:

protected override void OnNavigatedTo(NavigationEventArgs e) {
  if (e.Parameter != null) {
    IDataSourceExtension extension = (IDataSourceExtension)e.Parameter;
    pageTitle.Text = extension.Name;
    // Type of LastData: List<KeyValuePair<string,object>>
    listViewDataItems.ItemsSource = extension.LastData;                
  }
}

ItemsSource设置正常并包含KeyValuePairs,但在ListView中显示键和值不起作用。我在地铁风格的应用程序中使用.Net4.5 Beta。

1 个答案:

答案 0 :(得分:0)

我测试了这个:

public ObservableCollection<KeyValuePair<string, object>> LvItems { get; set; }

public MainWindow()
{
   InitializeComponent();
   this.DataContext = this;
   LvItems = new ObservableCollection<KeyValuePair<string, object>>();
   LvItems.Add(new KeyValuePair<string, object>("Idx", 5));
   LvItems.Add(new KeyValuePair<string, object>("Ido", 12));
}

并使用了你的itemtemplate,它就像一个魅力。

所以我最好的猜测是:

1)您需要使用ObservableCollection来通知集合中的更改(否则项目可能在此处,但ListView将不会刷新,因为它不知道集合已更改)。

2)你应该避免直接设置一个集合:只读它并逐个添加项目而不是设置集合。 (或在集合的setter中引发PropertyChanged。)

编辑:完整的代码。

<Window x:Class="ConverterCombinerTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:ConverterCombinerTest"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
         <ListView ItemsSource="{Binding LvItems}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Path=Key}"/>
                        <TextBlock Text=":"/>
                        <TextBlock Text="{Binding Path=Value}"/>
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
</Window>

public partial class MainWindow : Window, INotifyPropertyChanged
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = this;
            LvItems = new ObservableCollection<KeyValuePair<string, object>>();
            LvItems.Add(new KeyValuePair<string, object>("Idx", 5));
            LvItems.Add(new KeyValuePair<string, object>("Ido", 12));
        }

        public ObservableCollection<KeyValuePair<string, object>> LvItems { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;
        private void RaisePropertyChanged(String _Prop)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(_Prop));
            }
        }
    }