命令不能复选框

时间:2011-04-21 22:36:31

标签: mvvm prism

我使用过Prism来控制很多控件,但无法让它在复选框上运行。但它不适用于复选框。我注意到,当我在我的财产声明中放置断点时,它们被击中,所以它的某些部分是错误的。这是我的代码:

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>

1 个答案:

答案 0 :(得分:2)

所以我在这个链接上找到了答案blog.kevindockx.com/post/...基本上,当使用DataTemplate时,DataContext默认为直接父级(在我的情况下是ListBox)。所以你必须强制它回到ViewModel