Overblog Suivre ce blog
Editer l'article Administration Créer mon blog

Présentation

  • : Romagny13 - Du .NET,du pur .NET
  • Romagny13 - Du .NET,du pur .NET
  • : Cycle de progression Faire (quelque chose qui marche) -> comprendre ce que l’on fait/comment cela marche -> pousser plus loin les notions
  • Contact

Recherche

Articles Récents

4 mai 2014 7 04 /05 /mai /2014 17:54

http://wpftoolkit.codeplex.com/

 

BusyIndicator et ChildWindow

http://wpftoolkit.codeplex.com/wikipage?title=BusyIndicator
https://wpftoolkit.codeplex.com/wikipage?title=ChildWindow

 

Exemple simple, j’affiche une liste de villes et je permets l’édition de la ville courante dans une nouvelle fenêtre.

  •   BusyIndicator

mvvmprism3.png

  • ChilWindow

mvvmprism4.png



Namespace    xmlns:xctk=http://schemas.xceed.com/wpf/xaml/toolkit


  • BusyIndicator :je lie IsBusy à une propriété de mon ViewModel
  • ChildWindow : plusieurs propriétés intéressantes (caption,IsModal,WindowState,etc.)
  • Je lie l’état de la fenêtre (Open ou Closed) à une propriété de mon ViewModel
  • Pour la commande EditCityCommand, j’utilise RaiseCanExecuteChanged pour déclencher l'évaluation de la condition

<UserControl x:Class="Module1.Content1"

             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

             xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"

             mc:Ignorable="d"

             d:DesignHeight="300" d:DesignWidth="300">

    <Grid >

        <Grid.RowDefinitions>

            <RowDefinition Height="3*"/>

            <RowDefinition />

        </Grid.RowDefinitions>

        <xctk:BusyIndicator BusyContent="Loading Cities ..." IsBusy="{Binding IsBusy}" Grid.Row="0">

            <ListBox ItemsSource="{Binding Cities}"

                     DisplayMemberPath="CityName"

                     SelectedItem="{Binding SelectedCity}"

                     IsSynchronizedWithCurrentItem="True"

                     Margin="10"/>

        </xctk:BusyIndicator>

        <Button Command="{Binding EditCityCommand}" Grid.Row="1" Content="Edit city" Margin="10"/>

 

        <xctk:ChildWindow Grid.Row="0" Caption="Edition ..." IsModal="True" WindowStartupLocation="Center" WindowState="{Binding WindowState}">

            <Grid>

                <TextBox Text="{Binding SelectedCity.CityName}" />

            </Grid>

        </xctk:ChildWindow>

    </Grid>

</UserControl>

 

ViewModel

 

public class Content1ViewModel : IContent1ViewModel,INotifyPropertyChanged

    {

        public DelegateCommand EditCityCommand { get; set; }

        public ObservableCollection<City> Cities { get; set; }

 

        private Xceed.Wpf.Toolkit.WindowState _windowState;

 

        public Xceed.Wpf.Toolkit.WindowState WindowState

        {

            get { return _windowState; }

            set

            {

                _windowState = value;

                RaisePropertyChanged();

            }

        }

        private bool _isBusy;

 

        public bool IsBusy

        {

            get { return _isBusy; }

            set

            {

                _isBusy = value;

                RaisePropertyChanged();

            }

        }

 

        private City _selectedCity;

 

        public City SelectedCity

        {

            get { return _selectedCity; }

            set

            {

                _selectedCity = value;

                EditCityCommand.RaiseCanExecuteChanged();

                RaisePropertyChanged();

            }

        }

 

        public IView View { get; set; }

 

        public Content1ViewModel(IContent1View view)

        {

            this.View = view;

            this.View.ViewModel = this;

            EditCityCommand = new DelegateCommand(()=>

                WindowState = WindowState = Xceed.Wpf.Toolkit.WindowState.Open,

                ()=>SelectedCity!=null);

 

            LoadCities();

 

        }

 

        public async void LoadCities()

        {

            IsBusy = true;

            Cities = new ObservableCollection<City>(await GetAllAsync());

            RaisePropertyChanged("Cities");

            IsBusy = false;

        }

        // simule un long chargement asynchrone,pour bien faire il faudrait utiliser un service voir un repository

        public async Task<IList<City>> GetAllAsync()

        {

            IList<City> cities = new List<City>();

            cities.Add(new City() { CityName = "Londres" });

            await Task.Delay(1000);

            cities.Add(new City() { CityName = "Berlin" });

 

            return cities;

        }

  

        protected void RaisePropertyChanged([CallerMemberName] string propertyName = null)

        {

            var handler = PropertyChanged;

            if (handler != null)

                handler(this, new PropertyChangedEventArgs(propertyName));

 

        }

        public event PropertyChangedEventHandler PropertyChanged;

    }

    public class City :INotifyPropertyChanged

    {

        private string _cityName;

 

        public string CityName

        {

            get { return _cityName; }

            set

            {

                _cityName = value;

                RaisePropertyChanged();

            }

        }

 

        protected void RaisePropertyChanged([CallerMemberName] string propertyName = null)

        {

            var handler = PropertyChanged;

            if (handler != null)

                handler(this, new PropertyChangedEventArgs(propertyName));

 

        }

        public event PropertyChangedEventHandler PropertyChanged;

    }

 

Partager cet article

Repost 0
Published by Romagny13 - dans Prism
commenter cet article

commentaires