Командването не работи за отметка

Използвал съм Prism commanding за много контроли, но не мога да го накарам да работи в квадратчето за отметка. Но не работи за квадратчето за отметка. Забелязвам, че когато поставя точки на прекъсване в моите декларации за собственост, те биват ударени, така че част от тях е грешна. Ето моят код:

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;
        }
    }
}

Също така създавам няколко квадратчета за отметка от шаблон за данни вътре в списъчно поле

<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>

person Community    schedule 21.04.2011    source източник
comment
Така че намерих отговора на тази връзка blog.kevindockx.com/post/. По принцип, когато използвате DataTemplate, DataContext по подразбиране е непосредственият родител (в моя случай ListBox). Така че трябва да го принудите обратно към ViewModel.   -  person    schedule 22.04.2011
comment
Сега имам друг проблем. Командният ми параметър се връща нула. cmd:CheckBoxChecked.Command={Binding DataContext.ClickToAssociateUserCommand, ElementName=RootUserControl} cmd:CheckBoxChecked.CommandParameter={Binding Path=SelectedItem, ElementName=usersRoleAssociationsListBox}   -  person    schedule 22.04.2011


Отговори (1)


Така че намерих отговора на тази връзка blog.kevindockx.com/post/…. По принцип, когато използвате DataTemplate, DataContext по подразбиране е непосредственият родител (в моя случай ListBox). Така че трябва да го принудите обратно към ViewModel

person Community    schedule 05.05.2011