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

C# Discussion :

Erreur Binding Listbox.style


Sujet :

C#

  1. #1
    Membre du Club
    Inscrit en
    Février 2010
    Messages
    83
    Détails du profil
    Informations forums :
    Inscription : Février 2010
    Messages : 83
    Points : 50
    Points
    50
    Par défaut Erreur Binding Listbox.style
    Bonjour , j'ai implémenté une listBox pour que le contenu qui est un user controle peut être créer un certain nombre de fois. mais mon probleme est que je n'arrive pas a binder sur la textbox de mon userControle à partir de ma listbox.

    userControle avec ListBox
    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
     
                    <ListBox Grid.Row="1" Grid.ColumnSpan="2" Name="listNameProperties" DataContext="{Binding Path=DetailViewModel}" ItemsSource="{Binding Path= AllPropertiesName, Mode=oneWay, UpdateSourceTrigger=PropertyChanged}" SelectedItem="{Binding Path=PropertiesName}"  HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch"  ScrollViewer.VerticalScrollBarVisibility="Visible" ScrollViewer.HorizontalScrollBarVisibility="Disabled" SelectionChanged="listNameProperties_SelectionChanged" >
                        <ListBox.Style>
                            <Style TargetType="{x:Type ListBox}">
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding ElementName=tbName, Path=IsFocused}" Value="True">
     
     
                                        <Setter Property="Background" Value="Red"/>
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </ListBox.Style>
     
                        <ListBox.ItemTemplate>
     
                            <DataTemplate >
     
                                <view:ViewNameProperties  Margin="10"/>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>




    UserControl enfants ViewNameProperties
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
     
     
     
                <TextBlock Text="Name : " Grid.Row="0" Grid.Column="0" VerticalAlignment="Center" Margin="15"></TextBlock>
                <TextBox Grid.Column="1"  Grid.Row="0" Height="30" Margin="10" x:Name="tbName">
     
                    <TextBox.Text>
                        <Binding  Path="Name" UpdateSourceTrigger="PropertyChanged"  Mode="TwoWay"/>
                    </TextBox.Text>
     
                </TextBox>

    Voici l'erreur de Binding : System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=tbName'. BindingExpression: Path=IsFocused; DataItem=null; target element is 'ListBox' (Name='listNameProperties'); target property is 'NoTarget' (type 'Object')



    Merci d'avance

  2. #2
    Membre expert
    Avatar de GuruuMeditation
    Homme Profil pro
    .Net Architect
    Inscrit en
    Octobre 2010
    Messages
    1 705
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 49
    Localisation : Belgique

    Informations professionnelles :
    Activité : .Net Architect
    Secteur : Conseil

    Informations forums :
    Inscription : Octobre 2010
    Messages : 1 705
    Points : 3 568
    Points
    3 568
    Par défaut
    Qu'est-ce que tu veux faire exactement ? Quand le user met le focus sur un textbox de ta liste, il met le background en rouge, c'est ça?
    Microsoft MVP : Windows Platform

    MCPD - Windows Phone Developer
    MCPD - Windows Developer 4

    http://www.guruumeditation.net

    “If debugging is the process of removing bugs, then programming must be the process of putting them in.”
    (Edsger W. Dijkstra)

  3. #3
    Membre du Club
    Inscrit en
    Février 2010
    Messages
    83
    Détails du profil
    Informations forums :
    Inscription : Février 2010
    Messages : 83
    Points : 50
    Points
    50
    Par défaut
    oui mais je veux le faire dans la classe qui contient la listbox

  4. #4
    Membre expert
    Avatar de GuruuMeditation
    Homme Profil pro
    .Net Architect
    Inscrit en
    Octobre 2010
    Messages
    1 705
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 49
    Localisation : Belgique

    Informations professionnelles :
    Activité : .Net Architect
    Secteur : Conseil

    Informations forums :
    Inscription : Octobre 2010
    Messages : 1 705
    Points : 3 568
    Points
    3 568
    Par défaut
    A mon avis, il vaut mieux le faire via code, c'est plus simple (je pense).

    Le problème est que tu essayes de relier un controle avec des enfants crées dynamiquement via template. En XAML pure, je ne pense pas que ce soit possible.

    Tu peux retrouver le Listbox parent dans le code de ViewnameProperties comme ça :
    Code C# : 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
     
    public partial class UserControl1
        {
            public UserControl1()
            {
                InitializeComponent();
            }
     
            private void tbName_GotFocus(object sender, RoutedEventArgs e)
            {
                var listBox = GetListBoxParent();
     
                listBox.Background = new SolidColorBrush(Colors.Red);
            }
     
            private ListBox GetListBoxParent()
            {
                DependencyObject ucParent = this;
     
                while (!(ucParent is ItemsPresenter) && ucParent != null)
                {
                    ucParent = VisualTreeHelper.GetParent(ucParent);
     
     
                }
     
                return ucParent == null ? null : ((ItemsPresenter)ucParent).TemplatedParent as ListBox;
            }
        }

    Éventuellement tu peux mettre tout ça dans un behavior, pour être réutilisé plus facilement.
    Microsoft MVP : Windows Platform

    MCPD - Windows Phone Developer
    MCPD - Windows Developer 4

    http://www.guruumeditation.net

    “If debugging is the process of removing bugs, then programming must be the process of putting them in.”
    (Edsger W. Dijkstra)

Discussions similaires

  1. Binding ListBox - ComboBox
    Par sodaw dans le forum Windows Presentation Foundation
    Réponses: 10
    Dernier message: 26/10/2009, 09h52
  2. problème binding listbox!
    Par damii1 dans le forum Windows Presentation Foundation
    Réponses: 9
    Dernier message: 25/06/2009, 15h38
  3. Binding listbox avec datatemplate et récupération de valeur
    Par tom741 dans le forum Windows Presentation Foundation
    Réponses: 3
    Dernier message: 24/03/2009, 18h02
  4. erreur dans listbox
    Par philguio dans le forum Windows Forms
    Réponses: 6
    Dernier message: 06/10/2007, 20h10
  5. [VBA-E] erreur avec listbox.rowsource
    Par repié dans le forum Macros et VBA Excel
    Réponses: 3
    Dernier message: 14/02/2006, 10h55

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