【发布时间】:2014-01-31 05:47:27
【问题描述】:
我创建了一个附加属性,以使用元素样式的 datagridRow 或 datagricell 上的输入绑定来设置命令。
我在这里尝试在动态创建的东西(例如 datagridrows/cells)上添加 inputBinding,这就是我尝试在样式中添加 inputbinding 的原因。
不幸的是,样式不允许我在 xaml 中使用 datagridrows/cells 的 InputBindings 属性,它需要在代码中设置,这就是我需要附加属性的原因。
附加的属性propertychangedcallback方法在每行/单元格创建时触发,该方法在设置属性和属性值时接收对象。该值是输入绑定集合。在此集合中,项目存在,但这些项目的属性未绑定到它们的值
在 xaml 中,您可以看到我只为所有行/单元格创建了一个 InputBinding,但我需要在每一行上复制此 InputBinding,因为它们需要自己的 commandParameter,否则每行/单元格的 commandParameter 都相同
当我删除 InputBindingsCollection 并使用单个 InputBinding 时,它可以工作,但我只能设置 inputbinding 并且我想要鼠标绑定和键绑定,所以我需要使用 InputBindingsCollection。
当我使用 InputBindingsCollection 时,集合的 inputBindings 没有正确绑定
在 InputBindingsPropertyChangedCallBack 方法中,inpuBind.Command 和 inpuBind.CommandParameter 为 null 而不是绑定值
附加命令
public class AttacheCommand
{
public static DependencyProperty InputBindingsProperty = DependencyProperty.RegisterAttached("InputBindings",
typeof(InputBindingCollection),
typeof(AttacheCommand),
new PropertyMetadata(InputBindingsPropertyChangedCallBack));
public static void SetInputBindings(UIElement element, InputBindingCollection value)
{
element.SetValue(InputBindingsProperty, value);
}
public static InputBindingCollection GetInputBindings(UIElement element)
{
return (InputBindingCollection)element.GetValue(InputBindingsProperty);
}
public static void InputBindingsPropertyChangedCallBack(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
UIElement element = d as UIElement;
InputBinding copy;
foreach (InputBinding inpuBind in e.NewValue as InputBindingCollection)
{
//*****Here is my problem inpuBind.Command and inpuBind.CommandParameter are not bind to their value and are null
copy = new InputBinding(inpuBind.Command, inpuBind.Gesture);
copy.CommandParameter = inpuBind.CommandParameter;
element.InputBindings.Add(copy);
}
}
}
查看:
<Window x:Class="UltimateCommand.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:UltimateCommand"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<local:ViewModel x:Name="vm"/>
</Window.DataContext>
<Grid>
<DataGrid ItemsSource="{Binding listModel1}" IsReadOnly="True">
<DataGrid.RowStyle>
<Style TargetType="{x:Type DataGridRow}">
<Setter Property="local:AttacheCommand.InputBindings" >
<Setter.Value>
<InputBindingCollection>
<MouseBinding Command="{Binding ElementName=vm, Path=cmd1}" CommandParameter="{Binding}" Gesture="LeftClick"/>
</InputBindingCollection>
</Setter.Value>
</Setter>
</Style>
</DataGrid.RowStyle>
</DataGrid>
</Grid>
</Window>
ViewModel:(和命令)
class ViewModel
{
public Model1[] listModel1
{
get
{
return new Model1[] { new Model1("nom1", 1), new Model1("nom2", 2) };
}
}
public Command1 cmd1
{
get
{
return new Command1();
}
}
}
public class Command1 : ICommand
{
public bool CanExecute(object parameter)
{
//throw new NotImplementedException();
return true;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
//throw new NotImplementedException();
}
}
模型1:
public class Model1
{
public string Nom { get; set; }
public int Age { get; set; }
public Model1(string n, int a)
{
Nom = n;
Age = a;
}
}
【问题讨论】:
-
我猜你和这个人有同样的问题(见标记的答案)-social.msdn.microsoft.com/Forums/vstudio/en-US/…
-
天啊,你在这里做了很多不必要的工作......你不需要使用附加属性来设置
MouseBinding或KeyBindingICommand。只需使用RelayCommands 代替。然后您可以从视图模型中将数据绑定到Commands:<KeyBinding Gesture="CTRL+S" Command="{Binding Save, Mode=OneWay}" /> -
这里我也尝试在动态创建的东西上添加 inputBinding,例如 datagridrows/cells,这就是为什么我试图在样式中添加 inputbinding,该样式不允许使用 InputBindings 的属性xaml 中的 datagridrows/cells,它需要在代码中设置,不能使用您所说的
附加属性 propertychangedcallback 方法是在每个行/单元格创建时触发。
在 xaml 中,我只为所有行/单元格创建了一个 InputBinding,但我需要在每一行上复制 Inputbinding,因为它们需要自己的 commandParameter 但绑定不起作用
标签: c# .net wpf xaml attached-properties