-1

Hiding using Width would only hide it Visually, element is still there, what about screen reader.

  • I have GridViewColumn, where I want to show and hide based upon Property, How can I hide it.

  • Currently, I have using the this solution, However I want to do it using Property. How can I achieve it?

<GridViewColumn
                                CellTemplate="{StaticResource KeyTamplate}"
                                Header="{res:Label {x:Static res:enum.LabelName}}"
                                HeaderTemplate="{DynamicResource KeyHere}"
                                 />
Nexo
  • 2,125
  • 2
  • 10
  • 20

1 Answers1

1

Bind the Width property to a double property of the view model that you set to 0 to hide the column:

<GridViewColumn Header="header..." DisplayMemberBinding="{Binding Property}"
     Width="{Binding ColumnWidth}" />

View Model:

public double ColumnWidth { get; set; } = double.NaN;

If you set the ColumnWidth property dynamically, you should implement the INotifyPropertyChanged interface and raise the PropertyChanged event in the setter to notify the view.

Since you can't style a GridViewColumn, using a DataTrigger is not an option.

But you shouldn't really need one as you can set the source property directly in your view model and handle all the logic there.

mm8
  • 163,881
  • 10
  • 57
  • 88
  • Yes, I know this approach will work, but the tricky part is , I need to do it using ```DataTrigger```. – Nexo Jun 21 '23 at 14:09
  • You can't style a `GridViewColumn` so using a `DataTrigger` is not an option. But you shouldn't need one as you can set the view model property directly. – mm8 Jun 21 '23 at 14:10
  • Just implement your logic in the view model instead of using a `DataTrigger` in the view. – mm8 Jun 21 '23 at 14:19
  • What about setting ```DataTrigger``` to tamplates of ```cellTamplate``` and ```HeaderTamplate```. – Nexo Jun 21 '23 at 14:20
  • This won't affect the width or visibility of the column itself. And why do you want to to this in the view where you have a view model that you can set directly? It doesn't make any sense to me. – mm8 Jun 21 '23 at 14:21
  • I agree, its just because I was trying it to complete in this way. However now I think working with ```width``` is better approach. – Nexo Jun 21 '23 at 15:16
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/254179/discussion-between-nexo-and-mm8). – Nexo Jun 21 '23 at 15:22
  • Btw, what if someone is using screen reader to read, what will this hidden approach has affect? – Nexo Jun 21 '23 at 15:37
  • Please ask a new question if you have another issue. – mm8 Jun 22 '23 at 14:46
  • Hey, sorry, its not an issue, that the main reason, I have just updated my question. – Nexo Jun 22 '23 at 17:48
  • A screen reader cannot read an invisible element. – mm8 Jun 26 '23 at 15:16
  • We are setting width to 0, means element is still there. – Nexo Jun 26 '23 at 18:41
  • Depends on how you define "still there". It's not visible for a screen reader or a user. – mm8 Jun 27 '23 at 12:57