2013-08-30 44 views
1

空,我想在从ListView中派生,我们已经创建了一个控制禁用列reording。这种控制称为SortableListView。我认为一个依赖项属性将是实现这一点的最佳方式,但((SortableListVIew)source).View返回null。下面是代码:ListView.View是依赖属性

public class SortableListView : ListView 
{ 
    // ...lots of other properties here 

    public static readonly DependencyProperty AllowsColumnReorderProperty = 
     DependencyProperty.Register(
      "AllowsColumnReorder", 
      typeof(bool), 
      typeof(SortableListView), 
      new UIPropertyMetadata(true, AllowsColumnReorderPropertyChanged)); 

    public bool AllowsColumnReorder 
    { 
     get 
     { 
      return (bool)this.GetValue(AllowsColumnReorderProperty); 
     } 

     set 
     { 
      this.SetValue(AllowsColumnReorderProperty, value); 
     } 
    } 

    private static void AllowsColumnReorderPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e) 
    { 
     ViewBase vb = ((SortableListView)source).View; 

     if (vb != null) 
     { 
      ((GridView)vb).AllowsColumnReorder = (bool)e.NewValue; 
     } 
    } 

而XAML:

<TableControls:SortableListView x:Name="QueueViewTable" Margin="0,0,0,0" 
             Style="{StaticResource ListViewStyle}" 
             ItemsSource="{Binding Path=QueueList}" 
             ItemContainerStyle="{StaticResource alternatingListViewItemStyle}" 
             AlternationCount="2" 
             SelectionMode="Single" 
             SortEnabled="False" 
             AllowsColumnReorder="false"> 

麻烦的是,VB总是空,因此方法无法设置AllowsColumnReoder。我敢肯定,中投是有效的,因为最初的代码看起来像这样在OnInitialized:

((GridView)this.View).AllowsColumnReorder = false; 

...但我需要设置AllowsColumnReorder对视图的特定实例所以这个代码是没有好。

谁能告诉我我做错了什么?或者有更好的方法来设置这个属性?

回答

0

ListViewView财产本身是一个依赖属性可能会改变。当你设置你的财产时,它似乎没有设置还有

您可能必须重写可排序列表视图中的View属性,以便您可以添加属性更改侦听器,然后在视图本身设置时应用您的排序属性?

在WPF,您可以覆盖在父类中声明的依赖属性,如下所示:http://msdn.microsoft.com/en-us/library/ms754209.aspx

你会覆盖为View属性的元数据,并在您在此处设置PropertyMetadata参数,可以通过像你这样的功能是AllowsColumnReorderPropertyChanged

在那个处理程序中,你会检查新的视图是否是一个gridview,然后设置你的属性。

这样,AllowsColumnReorderView获得集合的任意顺序将正确设置你的财产。

+0

我不太确定我的理解。如何覆盖View属性并添加更改侦听器?对于愚蠢的问题抱歉,但我对WPF相当陌生,正在学习工作。 – serlingpa

+0

更新的答案包括更多信息 –

+0

非常感谢您的信息约翰。我还有一个问题:如何为我的AlowsColumnReorderPropertyChanged方法获得正确的Paramanter? – serlingpa