c# - how to stop a specific column to be generated in DataGrid when the AutoGenerateColumns is set to True? -
i have bound observablecollection datagrid
, set autogeneratecolumns
true in wpf mvvm application.
then how can stop
specific column
appeared in datagrid?
i have seen same question here. i'm looking more mvvm approach.
mvvm means ui , data layers separate, view layer merely being visual reflection of data layer.
so "mvvm way" of excluding column depends on exclusion should occur:
if data layer supposed exclude column, remove column data layer.
this typically means modifying collection
datagrid.itemssource
binding no longer includes data should not visible.or depending on application , counts "application logic", may mean maintaining
string
orlist<string>
of columns exclude, , have view find way of binding strings , modifying it's display exclude columns (a custom dependency property, converter, re-use oftag
property +autogeneratingcolumn
event, triggers, etc)if view layer supposed exclude column, remove column view layer.
this done setting
autogeneratecolumns="false"
, specifying<datagrid.columns>
yourself, can exclude column view layer using autogeneratingcolumn event ofdatagrid
, , cancelling generation of column if it'scolumnname
equals value want exclude, question linked suggested.private void datagrid_autogeneratingcolumn( object sender, datagridautogeneratingcolumneventargs e) { if ((string)e.column.header == "id") { e.cancel = true; } }
remember, whole point of mvvm separate ui , data layers. there's absolutely nothing wrong using code-behind view in mvvm, providing code related ui-specific logic only, , not data/application-specific logic
Comments
Post a Comment