Je suis en train de détecter quand un élément est vérifié et qui article est cochée dans une zone de liste à l'aide Silverlight 4 et le cadre Prism. Je found this example sur la création de comportements, et essayé de le suivre, mais rien ne se passe dans le débogueur. J'ai trois questions:CheckBox pour Silverlight Behaviors commande MVVM Motif
- Pourquoi ma commande ne s'exécute-t-elle pas?
- Comment déterminer quel élément a été vérifié (c'est-à-dire passer un paramètre de commande)?
- Comment est-ce que je débogue ceci? (Là où je peux mettre des points de rupture pour commencer d'entrer dans cette)
Voici mon code:
Vue:
<ListBox x:Name="MyListBox" ItemsSource="{Binding PanelItems, Mode=TwoWay}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding Enabled}" my:Checked.Command="{Binding Check}" />
<TextBlock x:Name="DisplayName" Text="{Binding DisplayName}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
ViewModel:
public MainPageViewModel()
{
_panelItems.Add(new PanelItem
{
Enabled = true,
DisplayName = "Test1"
});
Check = new DelegateCommand<object>(itemChecked);
}
public void itemChecked(object o)
{
//do some stuff
}
public DelegateCommand<object> Check { get; set; }
Comportement Classe
public class CheckedBehavior : CommandBehaviorBase<CheckBox>
{
public CheckedBehavior(CheckBox element)
: base(element)
{
element.Checked +=new RoutedEventHandler(element_Checked);
}
void element_Checked(object sender, RoutedEventArgs e)
{
base.ExecuteCommand();
}
}
classe Command
public static class Checked
{
public static ICommand GetCommand(DependencyObject obj)
{
return (ICommand) obj.GetValue(CommandProperty);
}
public static void SetCommand(DependencyObject obj, ICommand value)
{
obj.SetValue(CommandProperty, value);
}
public static readonly DependencyProperty CommandProperty =
DependencyProperty.RegisterAttached("Command", typeof(CheckBox), typeof(Checked), new
PropertyMetadata(OnSetCommandCallback));
public static readonly DependencyProperty CheckedCommandBehaviorProperty =
DependencyProperty.RegisterAttached("CheckedCommandBehavior", typeof(CheckedBehavior), typeof(Checked), null);
private static void OnSetCommandCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
CheckBox element = dependencyObject as CheckBox;
if(element != null)
{
CheckedBehavior behavior = GetOrCreateBehavior(element);
behavior.Command = e.NewValue as ICommand;
}
}
private static CheckedBehavior GetOrCreateBehavior(CheckBox element)
{
CheckedBehavior behavior = element.GetValue(CheckedCommandBehaviorProperty) as CheckedBehavior;
if(behavior == null)
{
behavior = new CheckedBehavior(element);
element.SetValue(CheckedCommandBehaviorProperty, behavior);
}
return behavior;
}
public static CheckedBehavior GetCheckCommandBehavior(DependencyObject obj)
{
return (CheckedBehavior) obj.GetValue(CheckedCommandBehaviorProperty);
}
public static void SetCheckCommandBehavior(DependencyObject obj, CheckedBehavior value)
{
obj.SetValue(CheckedCommandBehaviorProperty, value);
}
}
@Timores - Qu'est-ce que vous avez dit au sujet de la DataContext pour PanelItem ne pas être la même comme le ListBox a du sens. J'ai fait le changement comme vous l'avez suggéré et maintenant j'obtiens cette erreur: "Impossible de convertir le type Microsoft.Practices.Composite.Presentation.Commands.DelegateCommand'1 [System.Object] pour taper System.Windows.Controls.CheckBox." Si vous avez besoin de plus de code pour aider à résoudre les problèmes, s'il vous plaît faites le moi savoir et je vais essayer de vous l'obtenir. –
Ok, d'abord les bonnes nouvelles. Le premier commentaire que vous avez fait sur le 2ème paramètre corrigé le problème de type de conversion que j'avais. Maintenant, je suis en mesure de voir l'élément soit coché/décoché dans ma machine virtuelle! Je ne suis pas certain de comprendre votre deuxième commentaire. Est-ce la raison pour laquelle je ne peux pas déterminer quel élément a été coché/décoché? Comment puis-je déterminer quel élément de la liste a réellement été traité? J'espère que cela a du sens et j'apprécie vraiment votre aide jusqu'à présent! –
@Timores - Concernant l'édition deux: Si je pouvais obtenir la propriété DisplayName en tant que paramètre de commande qui serait idéal. Dans un exemple concret, j'aurais probablement une carte d'identité que je voudrais passer en arrière. –