2011-04-21 33 views
0

我已經使用棱鏡指揮了很多控件,但無法使其在複選框上工作。但它不適用於複選框。我注意到,當我在我的屬性聲明中放置斷點時,它們正在被擊中,所以它的一部分是錯誤的。這裏是我的代碼:內部列表框指揮不工作複選框

<ListBox x:Name="usersRoleAssociationsListBox" ItemsSource="{Binding UsersInRolesCollection}" 
               Height="180" 
               Width="220" 
               Margin="5,5,5,5"> 
             <ListBox.ItemTemplate> 
              <DataTemplate> 
               <StackPanel Orientation="Horizontal"> 
                <CheckBox 
                 IsChecked="{Binding IsAssociated}" 
                 cmd:CheckBoxChecked.Command="{Binding ClickToAssociateUserCommand}" 
                 cmd:CheckBoxChecked.CommandParameter="{Binding Path=SelectedItem, ElementName=usersRoleAssociationsListBox}"> 
                </CheckBox> 
                <TextBlock Text="{Binding UserName}"></TextBlock> 
               </StackPanel> 
              </DataTemplate> 
             </ListBox.ItemTemplate> 
            </ListBox> 
+1

所以我在這個鏈接上找到了答案http://blog.kevindockx.com/post/MVVM-challenge-binding-to-a-command-from-inside-an- ItemsControl的-DataTemplate.aspx。基本上當使用DataTemplate時,DataContext默認爲直接父級(在我的情況下是ListBox)。所以你必須強制它回到ViewModel。 – user583824 2011-04-22 03:51:31

+0

現在我有另一個問題。我的命令參數返回null。 cmd:CheckBoxChecked.Command =「{Binding DataContext.ClickToAssociateUserCommand,ElementName = RootUserControl}」 cmd:CheckBoxChecked.CommandParameter =「{Binding Path = SelectedItem,ElementName = usersRoleAssociationsListBox}」 – user583824 2011-04-22 04:09:48

回答

2

所以我發現這個鏈接blog.kevindockx.com/post/...答案

public class CheckBoxCommandBehavior : CommandBehaviorBase<CheckBox> 
{ 
    public CheckBoxCommandBehavior(CheckBox checkableObj) 
     : base(checkableObj) 
    { 
     checkableObj.Checked += new RoutedEventHandler(checkableObj_Checked); 
     checkableObj.Unchecked +=new RoutedEventHandler(checkableObj_Checked); 
    } 

    private void checkableObj_Checked(object s, RoutedEventArgs e) 
    { 
     ExecuteCommand(); 
    } 
} 

public static class CheckBoxChecked 
{ 
    private static readonly DependencyProperty CheckBoxCommandBehaviorProperty = DependencyProperty.RegisterAttached(
     "CheckBoxCommandBehavior", 
     typeof(CheckBoxCommandBehavior), 
     typeof(CheckBoxChecked), 
     null); 

    public static readonly DependencyProperty CommandProperty = DependencyProperty.RegisterAttached(
     "Command", 
     typeof(ICommand), 
     typeof(CheckBoxChecked), 
     new PropertyMetadata(OnSetCommandCallback)); 

    public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.RegisterAttached(
     "CommandParameter", 
     typeof(object), 
     typeof(CheckBoxChecked), 
     new PropertyMetadata(OnSetCommandParameterCallback)); 


    public static void SetCommand(CheckBox toggleBtn, ICommand cmd) 
    { 
     toggleBtn.SetValue(CommandProperty, cmd); 
    } 

    public static ICommand GetCommand(CheckBox toggleBtn) 
    { 
     return toggleBtn.GetValue(CommandProperty) as ICommand; 
    } 

    public static void SetCommandParameter(CheckBox selector, object parameter) 
    { 
     selector.SetValue(CommandParameterProperty, parameter); 
    } 

    public static object GetCommandParameter(CheckBox selector) 
    { 
     return selector.GetValue(CommandParameterProperty); 
    } 

    public static CheckBoxCommandBehavior GetOrCreateBehavior(CheckBox toggleBtn) 
    { 
     var behavior = toggleBtn.GetValue(CheckBoxCommandBehaviorProperty) as CheckBoxCommandBehavior; 

     if (behavior == null) 
     { 
      behavior = new CheckBoxCommandBehavior(toggleBtn); 
      toggleBtn.SetValue(CheckBoxCommandBehaviorProperty, behavior); 
     } 

     return behavior; 
    } 

    public static void OnSetCommandCallback(DependencyObject depObj, DependencyPropertyChangedEventArgs e) 
    { 
     var toggleBtn = depObj as CheckBox; 
     if (toggleBtn != null) 
     { 
      CheckBoxCommandBehavior behavior = GetOrCreateBehavior(toggleBtn); 
      behavior.Command = e.NewValue as ICommand; 
     } 
    } 

    private static void OnSetCommandParameterCallback(DependencyObject depObject, DependencyPropertyChangedEventArgs e) 
    { 
     var toggleBtn = depObject as CheckBox; 
     if (toggleBtn != null) 
     { 
      CheckBoxCommandBehavior behavior = GetOrCreateBehavior(toggleBtn); 
      behavior.CommandParameter = e.NewValue; 
     } 
    } 
} 

我還創建從一個DataTemplate幾個複選框。基本上當使用DataTemplate時,DataContext默認爲直接父級(在我的情況下是ListBox)。所以你必須強制它回到ViewModel