我在WPF中为我的MVVM项目创建了一个程序集。在我的程序集中,当我想对数据网格中的列进行排序时,我有一个激活行为。
行为SortColumn
public class SortColumn : Behavior<DataGrid>
{
public string Property = "";
public bool MeaningSort = true;
public static DependencyProperty AtSortingColumnCommandProperty = DependencyProperty.RegisterAttached(
"AtSortingColumnCommand", typeof(ICommand),
typeof(SortColumn));
public static ICommand GetAtSortingColumnCommand(DependencyObject obj)
{
return (ICommand)obj.GetValue(AtSortingColumnCommandProperty);
}
public static void SetAtSortingColumnCommand(DependencyObject obj, ICommand value)
{
obj.SetValue(AtSortingColumnCommandProperty, value);
}
protected override void OnAttached()
{
AssociatedObject.Sorting += AssociatedObject_Sorting;
base.OnAttached();
}
protected override void OnDetaching()
{
AssociatedObject.Sorting -= AssociatedObject_Sorting;
base.OnDetaching();
}
//Selon MeaningSort, on renvoie une chaine OrderBy en ASC ou DESC
//Ex: MonChamp ASC
private void AssociatedObject_Sorting(object sender, DataGridSortingEventArgs e)
{
FrameworkElement element = (FrameworkElement)sender;
string FiledName = e.Column.SortMemberPath;
if (Property == null || (Property != FiledName && MeaningSort != false))
{
e.Column.SortDirection = ListSortDirection.Ascending;
MeaningSort = false;
var atEnd = GetAtSortingColumnCommand(element);
if (atEnd != null)
{
atEnd.Execute(FiledName + " ASC");
}
}
else
{
e.Column.SortDirection = ListSortDirection.Descending;
MeaningSort = true;
var atEnd = GetAtSortingColumnCommand(element);
if (atEnd != null)
{
atEnd.Execute(FiledName + " DESC");
}
}
}
}
在我的XAML中
<D:DataGridTemplate x:Name="Datagrid"
TablePaged:ScrollViewerMonitor.AtEndCommand="{Binding LoadCommand}"
TablePaged:SortColumn.AtSortingColumnCommand="{Binding SortingColumnCommand}"
Grid.Column="0" Grid.Row="1" Grid.RowSpan="2" ItemsSource="{Binding DataProduits,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
所以只有当我的文件在同一个程序集中时,它才能正常工作。也许我不在同一个实例上?
答案 0 :(得分:3)
附加行为非常直观,应该按以下方式完成:
1)在文档标题
中包含以下xmlnsmax-age
2)定义行为所在的xmlns(也在文档标题中)
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
3)以下列方式将行为附加到元素:
xmlns:b="clr-namespace:MyAssembly.MyBehaviors;assembly=MyAssembly"
只要您按照这些步骤操作,行为就会附加,但请注意:
为了附加行为,附加行为的元素必须是行为的泛型参数指定的类型的子类(如果您使用泛型行为基类)。
或者,如果您使用非泛型 Behavior 基类,它必须是 DependencyObject 的子类。
编辑1:
你实现的行为是针对DataGrid的,但是你的XAML将它附加到DataGridTemplate类,所有这一切都是附加属性注册到你的行为而不是某些TargetType ..它真的似乎到处都是......
编辑2:
似乎你采用了附加的行为方法,这种方式使得子类化行为毫无意义..
如果您已经花时间将行为作为子类,那么您也可以将这些属性设置为常规依赖项属性。
答案 1 :(得分:0)
public class SortColumn
{
public static string Property = "";
public static bool MeaningSort = true;
public static DependencyProperty AtSortingColumnCommandProperty = DependencyProperty.RegisterAttached("AtSortingColumnCommand",
typeof(ICommand), typeof(SortColumn), new PropertyMetadata(OnAtSortingColumnCommandChanged));
public static ICommand GetAtSortingColumnCommand(DependencyObject obj)
{
return (ICommand)obj.GetValue(AtSortingColumnCommandProperty);
}
public static void SetAtSortingColumnCommand(DependencyObject obj, ICommand value)
{
obj.SetValue(AtSortingColumnCommandProperty, value);
}
public static void OnAtSortingColumnCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var myDataGrid = d as DataGrid;
if (myDataGrid != null)
{
myDataGrid.Sorting -= HandleColumnSorting;
myDataGrid.Sorting += HandleColumnSorting;
}
}
static void HandleColumnSorting(object sender, DataGridSortingEventArgs e)
{
var myDataGridSorting = sender as DataGrid;
string FiledName = e.Column.SortMemberPath;
if (Property == null || (Property != FiledName && MeaningSort != false))
{
e.Column.SortDirection = ListSortDirection.Ascending;
MeaningSort = false;
var atEnd = GetAtSortingColumnCommand(myDataGridSorting);
if (atEnd != null)
{
atEnd.Execute(FiledName + " ASC");
}
}
else
{
e.Column.SortDirection = ListSortDirection.Descending;
MeaningSort = true;
var atEnd = GetAtSortingColumnCommand(myDataGridSorting);
if (atEnd != null)
{
atEnd.Execute(FiledName + " DESC");
}
}
}
}