I have a DataGrid
that's bound to Datatable
, and I want to uniquely identify rows in the DataTable
using the ID
, but I don't want it to be shown in the DataGrid
What I reached so far by searching and excluding:
AutoGenerateColumns=True
, hence, I can't define the columns manually and set the Visibility
property to False
. List
or ObservableCollection
to define private ID
member, because the data are dynamic. MVVM
so, I can't use AutoGeneratingColumns
event handler directly and can't expose the View
to the ViewModel
. The closest I get to an answer is using DataTrigger
to set Visibility
to False
using CellStyle
, but it just hid the cells, not the entire column, and I also tried it for DataGridColumnHeader
and it didn't work:
code:
<Style x:Key="ColumnStyle" TargetType="DataGridColumnHeader">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Value}" Value="id">
<Setter Property="Visibility" Value="Hidden"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
How to do it while maintaining the previous conditions? Thanks in advance
I fixed the code for DataGridColumnHeader
using Path=Column.Header
which doesn't make sense to me but it's irrelevant; Still, there and empty column standing there, with no idea how to remove it.
It sounds like you want to track the selected Item. If you want to track the "selected Element", you have to use a CollectionView.
WPF controls do not direcly bind to collections. They bind to a CollectionView. And if you do not give them one, they will create one themself from whatever collection you hand them. If you want sorting, filtering, ordering and selection tracking, CollectionView is the droid you are looking for: https://msdn.microsoft.com/en-us/library/system.windows.data.collectionview.aspx
Just take control of it's creation and expose it (rather then the raw collection).
I found a solution by applying this style:
<Style x:Key="ColumnStyle" TargetType="DataGridColumnHeader">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Value}" Value="id">
<Setter Property="Visibility" Value="Hidden"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
to DataGridCell
and to DataGridColumnHeader
and allocating the column in the end of the table this removed the empty column from the middle of the table.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.