none
Binding to SelectedItems.Count in Metro

Answers

  • the property Count in 'SelectedItems.Count' does not notify the presentation layer. Hence, you can proceed as follows:

    <Page
        x:Class="App3.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:App3"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">
    
        <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
            <StackPanel>
                <TextBlock Text="{Binding Path=Count, Mode=OneWay}" Height="80" />
                <ListBox x:Name="listBox" SelectionMode="Multiple" 
                         ItemsSource="{Binding Path=ItemList}" SelectionChanged="listBox_SelectionChanged_1"/>
            </StackPanel>
        </Grid>
    </Page>

    -

    using System;
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Runtime.CompilerServices;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Navigation;
    
    namespace App3
    {
        /// <summary>
        ///
        /// </summary>
        public sealed partial class MainPage : Page, INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
            private bool SetProperty<T>(ref T storage, T value, [CallerMemberName] String propertyName = null)
            {
                if (object.Equals(storage, value)) return false;
                storage = value;
                this.OnPropertyChanged(propertyName);
                return true;
            }
            private void OnPropertyChanged([CallerMemberName] string propertyName = null)
            {
                var eventHandler = this.PropertyChanged;
                if (eventHandler != null)
                    eventHandler(this, new PropertyChangedEventArgs(propertyName));
            }
            ObservableCollection<string> itemList = new ObservableCollection<string>();
            public ObservableCollection<string> ItemList
            {
                get { return itemList; }
            }
            int count;
            public int Count
            {
                get { return count; }
                set { count = value; OnPropertyChanged(); }
            }
            public MainPage()
            {
                for (int i = 0; i < 4; i++)
                    itemList.Add(i.ToString());
                this.DataContext = this;
                this.InitializeComponent();
            }
            private void listBox_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
            {
                this.Count = listBox.SelectedItems.Count;
            }
        }
    }
    

     
    • Marked as answer by Jeff Eu Tuesday, September 04, 2012 1:10 PM
    Tuesday, September 04, 2012 12:56 PM

All replies

  • the property Count in 'SelectedItems.Count' does not notify the presentation layer. Hence, you can proceed as follows:

    <Page
        x:Class="App3.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:App3"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">
    
        <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
            <StackPanel>
                <TextBlock Text="{Binding Path=Count, Mode=OneWay}" Height="80" />
                <ListBox x:Name="listBox" SelectionMode="Multiple" 
                         ItemsSource="{Binding Path=ItemList}" SelectionChanged="listBox_SelectionChanged_1"/>
            </StackPanel>
        </Grid>
    </Page>

    -

    using System;
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Runtime.CompilerServices;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Navigation;
    
    namespace App3
    {
        /// <summary>
        ///
        /// </summary>
        public sealed partial class MainPage : Page, INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
            private bool SetProperty<T>(ref T storage, T value, [CallerMemberName] String propertyName = null)
            {
                if (object.Equals(storage, value)) return false;
                storage = value;
                this.OnPropertyChanged(propertyName);
                return true;
            }
            private void OnPropertyChanged([CallerMemberName] string propertyName = null)
            {
                var eventHandler = this.PropertyChanged;
                if (eventHandler != null)
                    eventHandler(this, new PropertyChangedEventArgs(propertyName));
            }
            ObservableCollection<string> itemList = new ObservableCollection<string>();
            public ObservableCollection<string> ItemList
            {
                get { return itemList; }
            }
            int count;
            public int Count
            {
                get { return count; }
                set { count = value; OnPropertyChanged(); }
            }
            public MainPage()
            {
                for (int i = 0; i < 4; i++)
                    itemList.Add(i.ToString());
                this.DataContext = this;
                this.InitializeComponent();
            }
            private void listBox_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
            {
                this.Count = listBox.SelectedItems.Count;
            }
        }
    }
    

     
    • Marked as answer by Jeff Eu Tuesday, September 04, 2012 1:10 PM
    Tuesday, September 04, 2012 12:56 PM
  • My temporary solution is very similar to yours, but I bound the TextBlock to a ViewModel's property which returns SelectedItems.Count, using MVVM would seem more clear.

    Ok I'll mark yours as answer.


    • Edited by Jeff Eu Tuesday, September 04, 2012 1:11 PM
    Tuesday, September 04, 2012 1:10 PM