IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Windows Presentation Foundation Discussion :

DataTemplate: binding sur un élément de la fenêtre parent


Sujet :

Windows Presentation Foundation

  1. #1
    Membre actif
    Profil pro
    Inscrit en
    Août 2006
    Messages
    582
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 582
    Points : 265
    Points
    265
    Par défaut DataTemplate: binding sur un élément de la fenêtre parent
    Bonjour,
    j'ai une listbox qui utilise un DataTemplate, ainsi qu'un slider dans ma fenêtre:
    Code xaml : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    <Window>
        <Grid>
            <Slider x:Name="sliderZoom" />
            <Listbox>
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Image Height="{Binding xxxxx}" Width="{Binding xxxxx}"  />
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </Listbox>
        </Grid>
    </Window>
    J'aimerais agrandir les Images de mon Template en fonction du slider (en utilisant un converter, non mentionné ici) et donc binder mes Image Height et Width à la Value du slider.
    Mais comment effectuer le binding sur un élément qui se trouve en dehors du DataTemplate ?

  2. #2
    Expert confirmé
    Inscrit en
    Avril 2008
    Messages
    2 564
    Détails du profil
    Informations personnelles :
    Âge : 64

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 564
    Points : 4 441
    Points
    4 441
    Par défaut
    bonjour
    Une prop d'un element UI encapsule dans un DataTemplate ne peut etre binde qu'à :
    -une prop d'un element source de Data (ObservableCollection source)
    -une prop du control parent (ListBox recevant le DataTemplate)
    -ou une prop d' un control constituant du control parent (ici ListBoxItem)...
    On ne peut binder le Slider qu'au ListBoxItem qui contient ton DataTemplate...
    Une autre circonstantce heureuse intervient car il faut zoomer uniquement le ListBoxItem selectionne..
    Ceci se fait via ItemContainerStyle du ListBox....
    Code exemple:
    1/code .cs du class data Person ;
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
     
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Collections.ObjectModel;
     
    namespace WpfZoomImage
    {
        public class Person
        {
            public Person()
            {
     
            }
            public Person(string n,Uri u ):this()
            {
                Name = n;
                ImageUri = u;
     
            }
            public String Name { get; set; }
            public Uri ImageUri { get; set; }
     
        }
        public class Persons : ObservableCollection<Person>
        {
            public Persons()
            {
                for (int i = 1; i < 6; i++)
                {
     
                    Person p = new Person(
                        "item"+i.ToString(),
                        new Uri("/Images/img"+i.ToString()+".jpg",UriKind.RelativeOrAbsolute ));
                    this.Add(p);
                }
     
            }
     
        }
     
    }
    code xaml du form:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
     
    <Window x:Class="WpfZoomImage.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:WpfZoomImage" 
            Title="MainWindow" Height="350" Width="525">
        <Window.Resources>
            <local:Persons x:Key="src"></local:Persons>
     
            <DataTemplate x:Key="dt" DataType="{x:Type local:Person}">
                <StackPanel Orientation="Horizontal"  >
                    <TextBox 
                       VerticalContentAlignment="Center" 
                       Text="{Binding Name}"  />
                    <Image 
                        Margin="5"
                        Source="{Binding ImageUri}">
                    </Image>
                </StackPanel>
            </DataTemplate>
        </Window.Resources>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="auto"></RowDefinition>
                <RowDefinition></RowDefinition>
            </Grid.RowDefinitions>
            <Slider 
                x:Name="sliderZoom" 
                Minimum="50" Maximum="300"
                Value="100"
                VerticalAlignment="Center"/>
            <ListBox
                Grid.Row="1"
                Background="Transparent" 
                ItemsSource="{Binding Source={StaticResource src}}"
                ItemTemplate="{StaticResource dt}"
                >
                <ListBox.ItemContainerStyle>
                    <Style TargetType="ListBoxItem">
                        <!--props defaut à ajuster suivant convenance-->
                        <Setter Property="Width" Value="100"/>
                        <Setter Property="Height" Value="100"/>
                        <Style.Triggers>
                            <Trigger Property="IsSelected" Value="True" >
                                <Setter Property = "Width" Value="{Binding ElementName=sliderZoom,Path=Value}"/>
                            </Trigger>
     
                            <Trigger Property="IsSelected" Value="True" >
                                <Setter Property = "Height" Value="{Binding ElementName=sliderZoom,Path=Value}"/>
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </ListBox.ItemContainerStyle>
            </ListBox>
        </Grid>
    </Window>
    code .cs du form (rien de special):
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
     
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
     
    namespace WpfZoomImage
    {
        /// <summary>
        /// Logique d'interaction pour MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
        }
    }
    bon code....

  3. #3
    Membre actif
    Profil pro
    Inscrit en
    Août 2006
    Messages
    582
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 582
    Points : 265
    Points
    265
    Par défaut
    Super! Merci pour le code.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réevaluer un binding sur ICollectionView quand un élément de la collection est modifié
    Par Deesmon dans le forum Windows Presentation Foundation
    Réponses: 0
    Dernier message: 28/02/2013, 15h34
  2. [XL-2003] Question sur les éléments de la fenêtre VBAProject
    Par hyperion13 dans le forum Macros et VBA Excel
    Réponses: 2
    Dernier message: 23/03/2010, 21h01
  3. Réponses: 5
    Dernier message: 30/01/2008, 09h18
  4. Réponses: 1
    Dernier message: 09/05/2007, 15h58
  5. Rafraîchissement sur un endroit de la fenêtre
    Par Mynautor dans le forum OpenGL
    Réponses: 5
    Dernier message: 07/06/2004, 14h47

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo