Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You best bet is probably to create a <strong>custom control</strong> by inheriting from the <strong>GridView</strong> class, adding the required columns, and exposing a meaningful property to show/hide a particular column. Your custom GridView class could look like this:</p> <pre><code>using System; using System.Windows.Controls; namespace MyProject.CustomControls { public class CustomGridView : GridView { private GridViewColumn _fixedColumn; private GridViewColumn _optionalColumn; public CustomGridView() { this._fixedColumn = new GridViewColumn() { Header = "Fixed Column" }; this._optionalColumn = new GridViewColumn() { Header = "Optional Column" }; this.Columns.Add(_fixedColumn); this.Columns.Add(_optionalColumn); } public bool ShowOptionalColumn { get { return _optionalColumn.Width &gt; 0; } set { // When 'False' hides the entire column // otherwise its width will be set to 'Auto' _optionalColumn.Width = (!value) ? 0 : Double.NaN; } } } } </code></pre> <p>Then you can simply set that property from XAML like in this example:</p> <pre><code>&lt;Window x:Class="WpfApplication1.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:cc="clr-namespace:MyProject.CustomControls" Title="Window1" Height="300" Width="300"&gt; &lt;StackPanel&gt; &lt;ListView&gt; &lt;ListView.View&gt; &lt;cc:CustomGridView ShowOptionalColumn="False" /&gt; &lt;/ListView.View&gt; &lt;/ListView&gt; &lt;ListView&gt; &lt;ListView.View&gt; &lt;cc:CustomGridView ShowOptionalColumn="True" /&gt; &lt;/ListView.View&gt; &lt;/ListView&gt; &lt;/StackPanel&gt; &lt;/Window&gt; </code></pre> <p>Optionally, you could make the '<em>CustomGridView.ShowOptionalColumn</em>' a <strong>DependencyProperty</strong> to be able to use it as a binding target.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload