Linking to ItemTemplate on KeyValuePair not working

I have a problem binding to work in the ItemTemplate of the ListView. My binding target is KeyValuePair. The following code:

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>

      

FROM#:

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;                
  }
}

      

The ItemsSource is fine-tuned and contains KeyValuePairs, but displaying the key and value in the ListView does not work. I am running .Net4.5 beta in metro style.

+3


source to share


1 answer


I tested this:

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));
}

      

And used your itemtemplate and it worked like a charm.

So I think that



1) You need to use an ObservableCollection to notify about changes in the collection (otherwise the items might be here, but the ListView is not updated as it doesn't know the collection has changed).

2) You should refrain from setting the collection directly: make it read-only and add items one at a time instead of setting the collection. (or raise PropertyChanged in the collection installer).

EDIT: complete code.

<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));
            }
        }
    }

      

0


source







All Articles