Mvvm 命令不为复选框工作
我已经在很多控件中使用了Prism命令,但无法在复选框中使用它。但它不适用于复选框。我确实注意到,当我在属性声明上放置断点时,它们被击中了,所以有些地方是错误的。这是我的密码:Mvvm 命令不为复选框工作,mvvm,prism,Mvvm,Prism,我已经在很多控件中使用了Prism命令,但无法在复选框中使用它。但它不适用于复选框。我确实注意到,当我在属性声明上放置断点时,它们被击中了,所以有些地方是错误的。这是我的密码: public class CheckBoxCommandBehavior : CommandBehaviorBase<CheckBox> { public CheckBoxCommandBehavior(CheckBox checkableObj) : base(checkableOb
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;
}
}
}
公共类CheckBoxCommandBehavior:CommandBehaviorBase
{
公共CheckBoxCommandBehavior(复选框checkableObj)
:基本(可检查对象)
{
checkableObj.Checked+=新路由拒绝服务器(checkableObj\u Checked);
checkableObj.Unchecked+=新路由拒绝服务器(checkableObj\u已选中);
}
私有无效检查对象已检查(对象s、路由目标e)
{
ExecuteCommand();
}
}
公共静态类CheckBoxChecked
{
私有静态只读DependencyProperty CheckBoxCommandBehaviorProperty=DependencyProperty.RegisterAttached(
“CheckBoxCommandBehavior”,
类型(CheckBoxCommandBehavior),
类型(复选框选中),
无效);
公共静态只读DependencyProperty CommandProperty=DependencyProperty.RegisterAttached(
“命令”,
类型(ICommand),
类型(复选框选中),
新属性元数据(OnSetCommandCallback));
公共静态只读DependencyProperty命令ParameterPerProperty=DependencyProperty.RegisterAttached(
“命令参数”,
类型(对象),
类型(复选框选中),
新属性元数据(OnSetCommandParameterCallback));
公共静态void SetCommand(复选框toggleBtn、ICommand和cmd)
{
toggleBtn.SetValue(CommandProperty,cmd);
}
公共静态ICommand GetCommand(复选框toggleBtn)
{
返回toggleBtn.GetValue(CommandProperty)作为ICommand;
}
公共静态void SetCommandParameter(复选框选择器,对象参数)
{
选择器.SetValue(CommandParameterProperty,参数);
}
公共静态对象GetCommandParameter(复选框选择器)
{
返回选择器.GetValue(CommandParameterProperty);
}
公共静态CheckBoxCommandBehavior GetOrCreateBhavior(复选框toggleBtn)
{
var behavior=toggleBtn.GetValue(CheckBoxCommandBehaviorProperty)作为CheckBoxCommandBehavior;
if(行为==null)
{
行为=新的CheckBoxCommandBehavior(toggleBtn);
toggleBtn.SetValue(CheckBoxCommandBehaviorProperty,behavior);
}
回归行为;
}
公共静态void OnSetCommandCallback(DependencyObject depObj、DependencyPropertyChangedEventArgs e)
{
var toggleBtn=depObj作为复选框;
if(toggleBtn!=null)
{
CheckBoxCommandBehavior=GetOrCreateBehavior(toggleBtn);
behavior.Command=e.NewValue作为ICommand;
}
}
私有静态void OnSetCommandParameterCallback(DependencyObject deObject,DependencyPropertyChangedEventArgs e)
{
var toggleBtn=deobject作为复选框;
if(toggleBtn!=null)
{
CheckBoxCommandBehavior=GetOrCreateBehavior(toggleBtn);
behavior.CommandParameter=e.NewValue;
}
}
}
我还从列表框中的datatemplate创建了几个复选框
<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,因此我在这个链接上找到了答案。基本上,当使用DataTemplate时,DataContext默认为直接父级(在我的例子中是ListBox)。所以你必须强制它回到视图模型。现在我有另一个问题。我的命令参数返回null。cmd:CheckBoxChecked.Command=“{Binding DataContext.ClickToAssociateUserCommand,ElementName=RootUserControl}”cmd:CheckBoxChecked.CommandParameter=“{Binding Path=SelectedItem,ElementName=usersRoleAssociationsListBox}”