Meilleur auteur de réponses
Executer une sub a l'intérieur d'une classe ajoutée a une ListBox

Question
-
Bonjour,Après trois heure de recherches veines (via google et par tâtonnement) j'en fait appel votre aide.Je développe en VB.net à l'aide de Visual Studio 2010.J'ai une ListBox dans laquelle je met des classes directement.J'utilise un DataTemplate pour tout placer correctement :
<DataTemplate> <StackPanel MinWidth="710"> <Image Source="folder.png" HorizontalAlignment="Left" VerticalAlignment="Top" /> <TextBlock Text="{Binding Path=FolderLocalName}" VerticalAlignment="Top" FontSize="20" FontFamily="Segoe UI" HorizontalAlignment="Left" Margin="37,-30,0,0"/> <TextBlock Text="{Binding Path=FolderSizeOnHardDisk}" VerticalAlignment="Top" HorizontalAlignment="Right" Margin="0,-35,0,0"/> <TextBlock Text="{Binding Path=FolderLocalPath}"/> <CheckBox x:Name="isconsyncchk" Content="Sauvegarde automatique" HorizontalAlignment="Right" Margin="0,-37,0,0" Checked="" Unchecked=""/> </StackPanel> </DataTemplate>
Je souhaiterai que les évements Checked et Unchecked déclanche des sub qui sont présentent dans la classe.Comment m'y prendre ?En vous remerciant de votre aide et en vous souhaitant une agréable journée.Bahaika.
Réponses
-
Bonjour,
Vous pouvez aussi faire cela(binder une commande sur un event) à l'aide des behaviors de blend: http://kishordaher.wordpress.com/200...avior-blend-3/
vous pouvez aussi lire cet article de mon blog qui présente une autre solution: http://blog.lexique-du-net.com/index...the-components
EDIT: je viens de voir que vous avez obtenu votre réponse sur un autre forum.
Bon courage,
Jonathan ANTOINE - http://wpf-france.fr - http://www.jonathanantoine.com- Proposé comme réponse JonathanANTOINEMVP, Moderator mardi 30 août 2011 12:02
- Marqué comme réponse JonathanANTOINEMVP, Moderator mardi 30 août 2011 12:02
-
Bonjour,
Pour moi votre problème peut être résolu par les commands.
Mais comme je ne suis pas doué en VB, je vais vous montrer un exemple en c#. (j'espère que vous pourrez le transformer en VB)
<Window x:Class="WpfApplication13.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApplication13" Title="MainWindow" Height="350" Width="525"> <Grid> <local:MyCheckBox x:Name="isconsyncchk2" Content="Sauvegarde automatique" RelayChecked="{Binding Path=MyPropChecked}" RelayUnchecked="{Binding Path=MyPropUnChecked}" /> </Grid> </Window>
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 WpfApplication13 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); MyClasse maclasse = new MyClasse(); this.DataContext = maclasse; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; using System.Windows.Input; using System.Windows; namespace WpfApplication13 { public class RelayCommand : ICommand { #region Variables private Action<Object, RoutedEventArgs> _execute; private Predicate<Object> _canExecute; private String _name = String.Empty; #endregion public String Name { get { return _name; } } #region Constructeurs public RelayCommand(Action<Object, RoutedEventArgs> execute) : this(execute, null) { } public RelayCommand(Action<Object, RoutedEventArgs> execute, Predicate<Object> canExecute) : this(String.Empty, execute, canExecute) { } public RelayCommand(String sName, Action<Object, RoutedEventArgs> execute) : this(sName, execute, null) { } public RelayCommand(String sName, Action<Object, RoutedEventArgs> execute, Predicate<Object> canExecute) { if (execute == null) throw new ArgumentNullException("execute"); _name = sName; _execute = execute; _canExecute = canExecute; } #endregion #region ICommand Members [DebuggerStepThrough] public Boolean CanExecute(Object parameter) { return _canExecute == null || _canExecute(parameter); } public event EventHandler CanExecuteChanged { add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; } } public void Execute(Object parameter) { _execute(parameter,null); } #endregion } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Controls; using System.Windows; namespace WpfApplication13 { public class MyCheckBox : CheckBox { public RelayCommand RelayChecked { get { return (RelayCommand)GetValue(RelayCheckedProperty); } set { SetValue(RelayCheckedProperty, value); } } // Using a DependencyProperty as the backing store for RelayChecked. This enables animation, styling, binding, etc... public static readonly DependencyProperty RelayCheckedProperty = DependencyProperty.Register("RelayChecked", typeof(RelayCommand), typeof(MyCheckBox), new UIPropertyMetadata(null)); public RelayCommand RelayUnchecked { get { return (RelayCommand)GetValue(RelayUncheckedProperty); } set { SetValue(RelayUncheckedProperty, value); } } // Using a DependencyProperty as the backing store for RelayUnchecked. This enables animation, styling, binding, etc... public static readonly DependencyProperty RelayUncheckedProperty = DependencyProperty.Register("RelayUnchecked", typeof(RelayCommand), typeof(MyCheckBox), new UIPropertyMetadata(null)); protected override void OnChecked(RoutedEventArgs e) { base.OnChecked(e); if (RelayChecked != null) RelayChecked.Execute(null); } protected override void OnUnchecked(RoutedEventArgs e) { base.OnUnchecked(e); if (RelayUnchecked != null) RelayUnchecked.Execute(null); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Input; using System.Windows.Controls; using System.Diagnostics; namespace WpfApplication13 { public class MyClasse { //public EventHandler MyChecked(object sender, RoutedEventArgs e); //public extern EventHandler MyUnChecked(object sender, RoutedEventArgs e); public RelayCommand MyPropChecked { get; set; } public RelayCommand MyPropUnChecked { get; set; } private void isconsyncchk_Checked(object sender, RoutedEventArgs e) { } private void isconsyncchk_Unchecked(object sender, RoutedEventArgs e) { } private CheckBox _chk = null; public MyClasse() { MyPropChecked = new RelayCommand(isconsyncchk_Checked); MyPropUnChecked = new RelayCommand(isconsyncchk_Unchecked); } } }
Cordialement, Pascal.
Développeur Wpf/SilverLight/WinPhone7
- Proposé comme réponse JonathanANTOINEMVP, Moderator mardi 30 août 2011 12:01
- Marqué comme réponse JonathanANTOINEMVP, Moderator mardi 30 août 2011 12:02
Toutes les réponses
-
Bonjour,
Vous y étiez presque, il suffit de mettre dans le xaml le nom de vos sub qui est dans votre classe et à condition que les subs respecte la signature de l'évènement checked et unchecked
Voici un exemple :
<DataTemplate> <StackPanel MinWidth="710"> <Image Source="folder.png" HorizontalAlignment="Left" VerticalAlignment="Top" /> <TextBlock Text="{Binding Path=FolderLocalName}" VerticalAlignment="Top" FontSize="20" FontFamily="Segoe UI" HorizontalAlignment="Left" Margin="37,-30,0,0"/> <TextBlock Text="{Binding Path=FolderSizeOnHardDisk}" VerticalAlignment="Top" HorizontalAlignment="Right" Margin="0,-35,0,0"/> <TextBlock Text="{Binding Path=FolderLocalPath}"/> <CheckBox x:Name="isconsyncchk" Content="Sauvegarde automatique" HorizontalAlignment="Right" Margin="0,-37,0,0" Checked="isconsyncchk_Checked" Unchecked="isconsyncchk_Unchecked"/> </StackPanel> </DataTemplate>
Private Sub isconsyncchk_Checked(sender As System.Object, e As System.Windows.RoutedEventArgs) End Sub Private Sub isconsyncchk_Unchecked(sender As System.Object, e As System.Windows.RoutedEventArgs) End Sub
Cordialement, Pascal.
Développeur Wpf/SilverLight/WinPhone7
-
Bonjour, merci pour votre réponse, cependant, je rencontre un soucis :
- Erreur 4 'isconsyncchk_Checked' n'est pas un membre de 'Projet.MainWindow'.
- Erreur 5 'isconsyncchk_Unchecked' n'est pas un membre de 'Projet.MainWindow'.
Il s'avère qu'il va chercher les fonctions directement dans la classe de la MainWindow, cependant, les objets que j'ajoute dans la ListBox sont des objets d'une classe que j'ai crée dans un fichier différent, j'aimerai appelé les sub du bon objet depuis la le contrôle présent dans la ListBox :La classe est présente dans un fichier séparé nommé FldrClass.vb, en voici le contenu :Public Class FolderClass Public Property FolderLocalPath As String = Nothing Public Property FolderLocalName As String = Nothing Public Property FolderSizeOnHardDisk As String = Nothing Public Property OrginalFolderSizeOnHardDisk As Long = Nothing Public Property IsContinueousSynced As Boolean = False Public Property ChkSC As Boolean = False Public Sub New(ByRef WantedFolderLocalPath As String) Blahblah End Sub Private Sub isconsyncchk_Checked(sender As System.Object, e As System.Windows.RoutedEventArgs) End Sub Private Sub isconsyncchk_Unchecked(sender As System.Object, e As System.Windows.RoutedEventArgs) End Sub End Class
Peut-être que je dois définir le type d'objet que j'ajoute dans le DataTemplate ?
Le DataTemplate est directement mis dans le propriété du ListBox, et j'ajoute les items de cette façon là :
For Each Item As String In ListArray Dim fldr As New FolderClass(Item) FldrListBox.Items.Add(fldr) Next
Auriez-vous une idée ? En vous souhaitant une bonne journée.
-
Bonjour,
Pour moi votre problème peut être résolu par les commands.
Mais comme je ne suis pas doué en VB, je vais vous montrer un exemple en c#. (j'espère que vous pourrez le transformer en VB)
<Window x:Class="WpfApplication13.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApplication13" Title="MainWindow" Height="350" Width="525"> <Grid> <local:MyCheckBox x:Name="isconsyncchk2" Content="Sauvegarde automatique" RelayChecked="{Binding Path=MyPropChecked}" RelayUnchecked="{Binding Path=MyPropUnChecked}" /> </Grid> </Window>
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 WpfApplication13 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); MyClasse maclasse = new MyClasse(); this.DataContext = maclasse; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; using System.Windows.Input; using System.Windows; namespace WpfApplication13 { public class RelayCommand : ICommand { #region Variables private Action<Object, RoutedEventArgs> _execute; private Predicate<Object> _canExecute; private String _name = String.Empty; #endregion public String Name { get { return _name; } } #region Constructeurs public RelayCommand(Action<Object, RoutedEventArgs> execute) : this(execute, null) { } public RelayCommand(Action<Object, RoutedEventArgs> execute, Predicate<Object> canExecute) : this(String.Empty, execute, canExecute) { } public RelayCommand(String sName, Action<Object, RoutedEventArgs> execute) : this(sName, execute, null) { } public RelayCommand(String sName, Action<Object, RoutedEventArgs> execute, Predicate<Object> canExecute) { if (execute == null) throw new ArgumentNullException("execute"); _name = sName; _execute = execute; _canExecute = canExecute; } #endregion #region ICommand Members [DebuggerStepThrough] public Boolean CanExecute(Object parameter) { return _canExecute == null || _canExecute(parameter); } public event EventHandler CanExecuteChanged { add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; } } public void Execute(Object parameter) { _execute(parameter,null); } #endregion } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Controls; using System.Windows; namespace WpfApplication13 { public class MyCheckBox : CheckBox { public RelayCommand RelayChecked { get { return (RelayCommand)GetValue(RelayCheckedProperty); } set { SetValue(RelayCheckedProperty, value); } } // Using a DependencyProperty as the backing store for RelayChecked. This enables animation, styling, binding, etc... public static readonly DependencyProperty RelayCheckedProperty = DependencyProperty.Register("RelayChecked", typeof(RelayCommand), typeof(MyCheckBox), new UIPropertyMetadata(null)); public RelayCommand RelayUnchecked { get { return (RelayCommand)GetValue(RelayUncheckedProperty); } set { SetValue(RelayUncheckedProperty, value); } } // Using a DependencyProperty as the backing store for RelayUnchecked. This enables animation, styling, binding, etc... public static readonly DependencyProperty RelayUncheckedProperty = DependencyProperty.Register("RelayUnchecked", typeof(RelayCommand), typeof(MyCheckBox), new UIPropertyMetadata(null)); protected override void OnChecked(RoutedEventArgs e) { base.OnChecked(e); if (RelayChecked != null) RelayChecked.Execute(null); } protected override void OnUnchecked(RoutedEventArgs e) { base.OnUnchecked(e); if (RelayUnchecked != null) RelayUnchecked.Execute(null); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Input; using System.Windows.Controls; using System.Diagnostics; namespace WpfApplication13 { public class MyClasse { //public EventHandler MyChecked(object sender, RoutedEventArgs e); //public extern EventHandler MyUnChecked(object sender, RoutedEventArgs e); public RelayCommand MyPropChecked { get; set; } public RelayCommand MyPropUnChecked { get; set; } private void isconsyncchk_Checked(object sender, RoutedEventArgs e) { } private void isconsyncchk_Unchecked(object sender, RoutedEventArgs e) { } private CheckBox _chk = null; public MyClasse() { MyPropChecked = new RelayCommand(isconsyncchk_Checked); MyPropUnChecked = new RelayCommand(isconsyncchk_Unchecked); } } }
Cordialement, Pascal.
Développeur Wpf/SilverLight/WinPhone7
- Proposé comme réponse JonathanANTOINEMVP, Moderator mardi 30 août 2011 12:01
- Marqué comme réponse JonathanANTOINEMVP, Moderator mardi 30 août 2011 12:02
-
Bonjour,
Est-ce que vous avez testé la solution proposée ? Merci de partager avec nous les résultats,afin que d'autres personnes avec le même problème puissent profiter de cette solution.
Cordialement, Pascal.
Développeur Wpf/SilverLight/WinPhone7
-
Bonjour,
Vous pouvez aussi faire cela(binder une commande sur un event) à l'aide des behaviors de blend: http://kishordaher.wordpress.com/200...avior-blend-3/
vous pouvez aussi lire cet article de mon blog qui présente une autre solution: http://blog.lexique-du-net.com/index...the-components
EDIT: je viens de voir que vous avez obtenu votre réponse sur un autre forum.
Bon courage,
Jonathan ANTOINE - http://wpf-france.fr - http://www.jonathanantoine.com- Proposé comme réponse JonathanANTOINEMVP, Moderator mardi 30 août 2011 12:02
- Marqué comme réponse JonathanANTOINEMVP, Moderator mardi 30 août 2011 12:02