C#/WPF:使 GridViewColumn Visible=false?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1392811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 16:00:26  来源:igfitidea点击:

C#/WPF: Make a GridViewColumn Visible=false?

c#wpflistviewvisibilitygridviewcolumn

提问by Joseph jun. Melettukunnel

Does anyone know if there is an option to hide a GridViewColumn somehow like this:

有谁知道是否有一个选项可以像这样隐藏 GridViewColumn:

<ListView.View>
    <GridView>
        <GridViewColumn Header="Test" IsVisible="{Binding Path=ColumnIsVisible}" />
    </GridView>
<ListView.View>

Thanks a lot!

非常感谢!

Edit: For clarity

编辑:为了清楚起见

Unfortunately, there is no "IsVisible" Property. I'm looking for a way to create that.

不幸的是,没有“IsVisible”属性。我正在寻找一种方法来创建它。

Edit: The solution based on the feedback looks like:

编辑:基于反馈的解决方案如下所示:

<GridViewColumn DisplayMemberBinding="{Binding Path=OptionColumn1Text}" 
                Width="{Binding Path=SelectedEntitiy.OptionColumn1Width}">
    <GridViewColumnHeader Content="{Binding Path=SelectedEntitiy.OptionColumn1Header}" IsEnabled="{Binding Path=SelectedEntitiy.OptionColumn1Width, Converter={StaticResource widthToBool}}" />
</GridViewColumn>

public class WidthToBooleanConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return (int)value > 0;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Thanks to all!
Cheers

谢谢大家!
干杯

采纳答案by Preet Sangha

Edit: Reflecting the modified question.

编辑:反映修改后的问题。

What about creating a 0 width column? Write a Boolean to Width IValueConverter, that takes a ColumnIsVisible as the ConverterParmeter?

创建一个 0 宽度的列怎么样?写一个布尔值到宽度 IValueConverter,它需要一个 ColumnIsVisible 作为 ConverterParmeter?

 public class BooleanToWidthConverter : IValueConverter {
        public object Convert(object value, Type targetType, 
                              object parameter, CultureInfo culture){
            return ((bool) parameter)? value : 0;
        }

        public object ConvertBack(object value, Type targetType, 
                                  object parameter, CultureInfo culture){
            throw new NotImplementedException();
        }
    }

Something like:

就像是:

<ListView .. >
 <ListView.Resources>
  <BooleanToWidthConverter x:Key="boolToWidth" />
 </ListView.Resources>

 <ListView.View>
    <GridView>
        <GridViewColumn 
                  Header="Test" 
                  Width=
      "{Binding Path=ColumnWidth, 
                Converter={StaticResource boolToVis}, 
                ConverterParameter=ColumnIsVisible}" />
    </GridView>
 <ListView.View>

回答by Sauron

Use if Thumb.DragDelta may solve the problem

如果 Thumb.DragDelta 可以解决问题,请使用

I use it in listview as

我在列表视图中使用它作为

<ListView x:Name="MyListView"IsSynchronizedWithCurrentItem="True"   
      ItemsSource="{Binding Path=Items}",  Mode=Default, Source={StaticResource DataProvider}}" 
      Thumb.DragDelta="Thumb_DragDelta">


public Window1()
{   
InitializeComponent(); 
MyListView.AddHandler(Thumb.DragDeltaEvent, new DragDeltaEventHandler(Thumb_DragDelta), true );

void Thumb_DragDelta(object sender, DragDeltaEventArgs e)
{  
 Thumb senderAsThumb = e.OriginalSource as Thumb;    
 GridViewColumnHeader header = senderAsThumb.TemplatedParent as GridViewColumnHeader;     
 if (header.Column.ActualWidth < MIN_WIDTH)   
 {   
    header.Column.Width = MIN_WIDTH;  
 }  
 if (header.Column.ActualWidth > MAX_WIDTH)     
 {      
    header.Column.Width = MAX_WIDTH;   
 }
}
}

回答by Helge Klein

Hereis another solution based on setting the column's width to zero. I have modified it a little. It now works like this:

是基于将列的宽度设置为零的另一种解决方案。我稍微修改了一下。它现在是这样工作的:

  1. Bind the header's visibility to a boolean property of the ViewModel, using a bool-to-visibility converter
  2. Use an attached property on the header to set the column's width to zero
  1. 使用 bool-to-visibility 转换器将标头的可见性绑定到 ViewModel 的布尔属性
  2. 使用标题上的附加属性将列的宽度设置为零

Here is the code.

这是代码。

XAML:

XAML:

<GridViewColumn
    HeaderTemplate="..." 
    HeaderContainerStyle="...">
    <GridViewColumnHeader 
        Content="Header text" 
        Visibility="{Binding AppliesToColumnVisible, Converter={StaticResource BooleanToVisibilityConverter}}" 
        behaviors:GridViewBehaviors.CollapseableColumn="True" />

BooleanToVisibilityConverter:

BooleanToVisibilityConverter:

public class BooleanToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        bool param = bool.Parse(value.ToString());
        if (param == true)
            return Visibility.Visible;
        else
            return Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Attached behavior GridViewBehaviors.CollapseableColumn:

附加行为 GridViewBehaviors.CollapseableColumn:

public static readonly DependencyProperty CollapseableColumnProperty =
     DependencyProperty.RegisterAttached("CollapseableColumn", typeof(bool), typeof(GridViewBehaviors),
    new UIPropertyMetadata(false, OnCollapseableColumnChanged));

public static bool GetCollapseableColumn(DependencyObject d)
{
    return (bool)d.GetValue(CollapseableColumnProperty);
}

public static void SetCollapseableColumn(DependencyObject d, bool value)
{
    d.SetValue(CollapseableColumnProperty, value);
}

private static void OnCollapseableColumnChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
{
    GridViewColumnHeader header = sender as GridViewColumnHeader;
    if (header == null)
        return;

    header.IsVisibleChanged += new DependencyPropertyChangedEventHandler(AdjustWidth);
}

static void AdjustWidth(object sender, DependencyPropertyChangedEventArgs e)
{
    GridViewColumnHeader header = sender as GridViewColumnHeader;
    if (header == null)
        return;

    if (header.Visibility == Visibility.Collapsed)
        header.Column.Width = 0;
    else
        header.Column.Width = double.NaN;   // "Auto"
}

回答by hyphestos

I've set the Column the Width="0" to zero. Now the column looks like its not visible. But i do not know if it will affect anything else. It might be a dummy solution but for now it works.

我已将 Column 的 Width="0" 设置为零。现在该列看起来不可见。但是不知道会不会影响其他。这可能是一个虚拟的解决方案,但现在它有效。

回答by stuicidle

One simpler approach, that still uses the concept of setting the columns width to zero but does not have the side effects of using a IValueConverter(the user can still drag the column wider) is to create a new getter/setter that returns a width based on your ColumnIsVisiblevariable and then bind to that:

一种更简单的方法,它仍然使用将列宽设置为零的概念,但没有使用 a 的副作用IValueConverter(用户仍然可以将列拖得更宽)是创建一个新的 getter/setter,它返回基于你的ColumnIsVisible变量,然后绑定到:

public double ColumnWidth
{
    get
    {
        if (this.ColumnIsVisible)
        {
            return 100;
        }
        else
        {
            return 0;
        }
    }

    set
    {
        OnPropertyChanged("ColumnWidth");
    }
}

Make your bindings TwoWay and if the user attempts to drag the column wider OnPropertyChangedwill be called and reset the width to 0. You might have to use a binding proxy though for your binding. Also add a call to OnPropertyChanged("ColumnWidth")when ever ColumnIsVisible is updated :)

以双向方式进行绑定,如果用户尝试将列拖得更宽,OnPropertyChanged则会调用并将宽度重置为 0。不过,您可能必须使用绑定代理进行绑定。还可以OnPropertyChanged("ColumnWidth")在 ColumnIsVisible 更新时添加调用:)