我有一個使用AutoGenerateColumns = True綁定到DataTable的WPF 4.0 DataGrid。這些列是動態的,但是我知道總會有一個名爲ID的列,我想隱藏這個列。有什麼辦法可以做到這一點?有沒有辦法在AutoGenerateColumns = True時隱藏DataGrid中的特定列?
回答
,認購AutoGeneratingColumn
事件,事件args
(DataGridAutoGeneratingColumnEventArgs
)的列名和「Cancel
」,如果列名是ID然後設置Cancel = true
。應該做的伎倆。
我不能說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
名稱。
我不會說這是很好的解決方案...但是...你可以有例如多了一個抽象層 假設你有一個像對象:
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列要麼。
另一種可能性是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;
}
}
您可以使用行爲(可重用的代碼),做的工作......這樣你就可以使用屬性,它會集中在一個地方的知名度列。
用法:
<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;
}
}
}
}
}
}
}
這正是我正在尋找的!經測試並在.NET 4.6.1上工作:) – JoanComasFdz 2016-11-23 14:02:17
謝謝!很高興知道它仍然工作正常:)! – 2016-11-23 16:52:32
DataGrid應該以這種方式開箱即用,並從DisplayNameAttriibute中挑選標題! – StuartQ 2017-12-12 17:39:03
- 1. 如何在AutoGenerateColumns =「true」時隱藏列GridView?
- 2. 當AutoGenerateColumns =「True」時,如何使datagrid的特定列成爲combobox
- 3. 有沒有辦法阻止TJvDocServerForm在未固定時隱藏?
- 4. 有沒有辦法隱藏主要活動的特定列表視圖項目?
- 5. 有沒有辦法在Keystone.js中隱藏ID列?
- 6. 有沒有辦法隱藏JavaScript代碼?
- 7. 有沒有辦法隱藏'System.Windows.Forms.ListBox()'邊框?
- 8. 有沒有辦法隱藏Javascript評論?
- 9. 有沒有辦法隱藏Vaadin通知?
- 10. 有沒有辦法隱藏UIDatePicker中的24小時制格式
- 11. 有沒有辦法在傳遞數據框時隱藏rhandsontable中的列
- 12. 當沒有記錄時隱藏DataGrid
- 13. 有沒有辦法在MySQL中鎖定特定的行和列?
- 14. 有沒有辦法在AppCode中隱藏cocoapods中的警告?
- 15. 如果AutoGenerateColumns爲false,DataGrid沒有正確綁定到DataGrid
- 16. 有沒有辦法在全屏時完全隱藏YouTube控件?
- 17. 有沒有辦法在子類中隱藏部分方法?
- 18. 有沒有辦法隱藏/自定義ChartJS上面的圖例?
- 19. 有沒有辦法隱藏/刪除特定的woocommerce管理菜單?
- 20. 有沒有辦法讓NetBeans隱藏特定的文件/文件夾?
- 21. 有沒有辦法隱藏沒有CSS的按鈕值?
- 22. 有沒有辦法顯示/隱藏沒有CSS的元素?
- 23. mvc3 .net - 有沒有辦法在視圖中完全隱藏隱藏字段?
- 24. 有沒有辦法在HighCharts中隱藏其中一個堆疊系列的dataLabels
- 25. 有沒有辦法在Sublime Text 2中隱藏側欄?
- 26. 有沒有辦法在Ace Editor中隱藏垂直標尺?
- 27. 在Xul或JavaScript中,有沒有辦法隱藏鼠標光標?
- 28. 有沒有什麼辦法在IntelliJ 9中隱藏面板?
- 29. 有沒有辦法在PowerShell中隱藏輸出?
- 30. 有沒有辦法在Netbeans或Eclipse中隱藏註釋?
這會工作...儘管它立即違背了MVVM模式......但毫無疑問會起作用。 – 2010-10-22 19:29:46
謝謝,這將很好地工作。在這種情況下,我並不太關心MVVM,因爲DataGrid是自定義UserControl的一部分,並且如果僅影響View,那麼使用後臺代碼就沒有問題。 – Rachel 2010-10-22 19:33:59
@Aaron,如果你依賴視圖來生成MV,那麼我認爲你會發現它不符合MVVM :) – 2010-10-22 19:34:53