2010-10-22 30 views

回答

30

,認購AutoGeneratingColumn事件,事件argsDataGridAutoGeneratingColumnEventArgs)的列名和「Cancel」,如果列名是ID然後設置Cancel = true。應該做的伎倆。

+0

這會工作...儘管它立即違背了MVVM模式......但毫無疑問會起作用。 – 2010-10-22 19:29:46

+4

謝謝,這將很好地工作。在這種情況下,我並不太關心MVVM,因爲DataGrid是自定義UserControl的一部分,並且如果僅影響View,那麼使用後臺代碼就沒有問題。 – Rachel 2010-10-22 19:33:59

+0

@Aaron,如果你依賴視圖來生成MV,那麼我認爲你會發現它不符合MVVM :) – 2010-10-22 19:34:53

1

我不能說4,但是在3.5 SP1中是不可能的,至少沒有註冊我想要不惜一切代價避免的事件。

你可以做什麼,而不是是改變你的代碼AutoGenerateColumns=False然後只需將你所關心的XAML中的基礎數據都將仍然被放置在列中的列適當

  <dg:DataGridTextColumn Header="Display" Binding="{Binding DisplayName}"/> 
      <dg:DataGridTextColumn Header="Host" Binding="{Binding HostName}"/> 
      <dg:DataGridTextColumn Header="Database" Binding="{Binding Database}"/> 
      <dg:DataGridTextColumn Header="Username" Binding="{Binding Username}"/> 
      <dg:DataGridTextColumn Header="Password" Binding="{Binding Password}"/> 

這將使您可以顯示您關心的底層模型的唯一列,並根據需要將Header更改爲顯示,因此您不會綁定到模型上的Property名稱。

在數據網格
+0

這不是一個選項,我,因爲我不知道列名事先 – Rachel 2010-10-22 19:22:42

+0

這是不是從模型生成的?數據是什麼樣的......? – 2010-10-22 19:25:27

+0

該模型只包含一個DataTable屬性,其中包含要在DataGrid中顯示的數據。 – Rachel 2010-10-22 19:31:06

0

我不會說這是很好的解決方案...但是...你可以有例如多了一個抽象層 假設你有一個像對象:

public class Foo 
{ 
    public string Id { get; set; } 

    public string Property2 { get; set; } 

    public string Property3 { set; get; } 
} 

你不希望列爲Id,所以你創建新的對象

public class Foo2 
{ 
    public string Property2 { get; set; } 

    public string Property3 { set; get; } 
} 

然後映射/轉換Foo到Foo2,你就完成了。

(並不總是可能),另一種可能的方式是訪問修飾符更改爲內部

public class Foo 
{ 
    internal string Id { get; set; } 

    public string Property2 { get; set; } 

    public string Property3 { set; get; } 
} 

這樣你就不必生成的ID列要麼。

2

另一種可能性是Visibility.Collapsed

private void dataGrid_AutoGeneratingColumn(object sender, 
     DataGridAutoGeneratingColumnEventArgs e) 
    { 
     //Set properties on the columns during auto-generation 
     switch (e.Column.Header.ToString()) 
     { 
      case "rownameYouWantToHide": 
       e.Column.Visibility = Visibility.Collapsed; 
       break; 
     } 
    } 
6

您可以使用行爲(可重用的代碼),做的工作......這樣你就可以使用屬性,它會集中在一個地方的知名度列。

用法:

<Window 
... 
xmlns:extension="clr-namespace:WpfControlLibrary.Extension;assembly=WpfControlLibrary"> 

<DataGrid ... 
    extension:DataGridBehavior.UseBrowsableAttributeOnColumn="True"> 

...

public class YourObjectItem 
{ 
    [Browsable(false)] 
     public Assembly Assembly { get; set; } 

代碼:

using System; 
using System.ComponentModel; 
using System.Windows; 
using System.Windows.Controls; 

namespace WpfControlLibrary.Extension 
{ 
    public static class DataGridBehavior 
    { 
     public static readonly DependencyProperty UseBrowsableAttributeOnColumnProperty = 
      DependencyProperty.RegisterAttached("UseBrowsableAttributeOnColumn", 
      typeof(bool), 
      typeof(DataGridBehavior), 
      new UIPropertyMetadata(false, UseBrowsableAttributeOnColumnChanged)); 

     public static bool GetUseBrowsableAttributeOnColumn(DependencyObject obj) 
     { 
      return (bool)obj.GetValue(UseBrowsableAttributeOnColumnProperty); 
     } 

     public static void SetUseBrowsableAttributeOnColumn(DependencyObject obj, bool val) 
     { 
      obj.SetValue(UseBrowsableAttributeOnColumnProperty, val); 
     } 

     private static void UseBrowsableAttributeOnColumnChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) 
     { 
      var dataGrid = obj as DataGrid; 
      if (dataGrid != null) 
      { 
       if ((bool) e.NewValue) 
       { 
        dataGrid.AutoGeneratingColumn += DataGridOnAutoGeneratingColumn; 
       } 
       else 
       { 
        dataGrid.AutoGeneratingColumn -= DataGridOnAutoGeneratingColumn; 
       } 
      } 
     } 

     private static void DataGridOnAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e) 
     { 
      var propDesc = e.PropertyDescriptor as PropertyDescriptor; 

      if (propDesc != null) 
      { 
       foreach(Attribute att in propDesc.Attributes) 
       { 
        var browsableAttribute = att as BrowsableAttribute; 
        if (browsableAttribute != null) 
        { 
         if (! browsableAttribute.Browsable) 
         { 
          e.Cancel = true; 
         } 
        } 
       } 
      } 
     } 
    } 
} 
+0

這正是我正在尋找的!經測試並在.NET 4.6.1上工作:) – JoanComasFdz 2016-11-23 14:02:17

+0

謝謝!很高興知道它仍然工作正常:)! – 2016-11-23 16:52:32

+0

DataGrid應該以這種方式開箱即用,並從DisplayNameAttriibute中挑選標題! – StuartQ 2017-12-12 17:39:03

相關問題