【问题标题】:Commanding not working for checkbox命令不适用于复选框
【发布时间】:2011-04-21 22:36:31
【问题描述】:

我已将 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>

【问题讨论】:

  • 所以我在这个链接blog.kevindockx.com/post/…上找到了答案。基本上,当使用 DataTemplate 时,DataContext 默认为直接父级(在我的情况下为 ListBox)。所以你必须强制它回到 ViewModel。
  • 现在我还有一个问题。我的命令参数返回为空。 cmd:CheckBoxChecked.Command="{Binding DataContext.ClickToAssociateUserCommand, ElementName=RootUserControl}" cmd:CheckBoxChecked.CommandParameter="{Binding Path=SelectedItem, ElementName=usersRoleAssociationsListBox}"

标签: mvvm prism


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-10
    • 2019-10-12
    • 2013-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多