Dav*_*ave 2 wpf datagrid devexpress mvvm mvvm-light
我目前正在好好看看Laurent 的优秀工具包,我有以下问题.
从Blend 4中,我为Loaded事件添加了一个EventTrigger,在我的ViewModel中,我有以下内容:
public RelayCommand rcAutoGeneratingColumn { get; private set; }
Run Code Online (Sandbox Code Playgroud)
在构造函数中我有:
rcAutoGeneratingColumn =
new RelayCommand(o => DataGridAutoGeneratingColumn(o));
Run Code Online (Sandbox Code Playgroud)
同样在ViewModel中,我有我希望由RelayCommand调用的方法:
private void DataGridAutoGeneratingColumn(Object o)
{
DataGrid grid = (DataGrid)o;
foreach (DataGridTextColumn col in grid.Columns)
{
if (col.Header.ToString().ToLower() == "id")
{
col.Visibility = System.Windows.Visibility.Hidden;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的XAML包含以下内容(对于DataGrid):
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<GalaSoft_MvvmLight_Command:EventToCommand
Command="{Binding rcAutoGeneratingColumn, Mode=OneWay}"
CommandParameter="{Binding ElementName=dataGrid1, Mode=OneWay}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
Run Code Online (Sandbox Code Playgroud)
这里没有问题代码工作正常,但显然用于隐藏某些列的事件应该是AutoGeneratingColumn事件而不是Loaded.我曾经习惯将Loaded事件作为一个问题.
我希望我可以转发控件提供的任何事件,这样,在这种情况下,以下代码将起作用:
<i:Interaction.Triggers>
<i:EventTrigger EventName="AutoGeneratingColumn">
<GalaSoft_MvvmLight_Command:EventToCommand
Command="{Binding rcAutoGeneratingColumn, Mode=OneWay}"
CommandParameter="{Binding ElementName=dataGrid1, Mode=OneWay}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
Run Code Online (Sandbox Code Playgroud)
我无法触发AutoGeneratingColumn事件,我希望我忽略了一些事情,并感谢任何给出的建议!
这种行为与DevExpress中的GridControl相同,因为Loaded事件被触发,而ColumnsPopulated事件(这相当于AutoGeneratingColumn事件)则不然.
DevExpress就我的问题提供了以下信息:
" 我们已经回顾了这个问题,并得出了一个有趣的结论.看起来,当处理Interaction.Triggers时,可视树不会被构建 "
如果这是真的,并且没有其他方法可以调用ViewModel中的事件,那么就必须继续 - 通过使用试验和错误 - 注意哪些DataGrid事件(其中有超过100个)可以用这种方式调用哪些不可以!
人们可能会认为,在应用MVVM模式时,也可以在代码隐藏中使用每个事件.
我已经找到了答案,但我不能排除我忽略了一些事情,所以如果是这样的话,请接受我的道歉!
Rus*_*ngs 12
您不必使用背后的恶意代码;-)您可以使用附加行为来执行此操作...
public class AutoGeneratingColumnEventToCommandBehaviour
{
public static readonly DependencyProperty CommandProperty =
DependencyProperty.RegisterAttached(
"Command",
typeof(ICommand),
typeof(AutoGeneratingColumnEventToCommandBehaviour),
new PropertyMetadata(
null,
CommandPropertyChanged));
public static void SetCommand(DependencyObject o, ICommand value)
{
o.SetValue(CommandProperty, value);
}
public static ICommand GetCommand(DependencyObject o)
{
return o.GetValue(CommandProperty) as ICommand;
}
private static void CommandPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var dataGrid = d as DataGrid;
if (dataGrid != null)
{
if (e.OldValue != null)
{
dataGrid.AutoGeneratingColumn -= OnAutoGeneratingColumn;
}
if (e.NewValue != null)
{
dataGrid.AutoGeneratingColumn += OnAutoGeneratingColumn;
}
}
}
private static void OnAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
var dependencyObject = sender as DependencyObject;
if (dependencyObject != null)
{
var command = dependencyObject.GetValue(CommandProperty) as ICommand;
if (command != null && command.CanExecute(e))
{
command.Execute(e);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后像这样在XAML中使用它......
<DataGrid ItemsSource="{Binding MyGridSource}"
AttachedCommand:AutoGeneratingColumnEventToCommandBehaviour.Command="{Binding CreateColumnsCommand}">
</DataGrid>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5602 次 |
| 最近记录: |