C#/ WPF:使GridViewColumn可见= false?

时间:2009-09-08 08:54:59

标签: c# wpf listview visibility gridviewcolumn

有没有人知道是否有一个隐藏GridViewColumn的选项:

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

非常感谢!

编辑:为清晰起见

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

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

<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();
    }
}

感谢所有人!
干杯

5 个答案:

答案 0 :(得分:10)

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

创建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();
        }
    }

类似的东西:

<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>

答案 1 :(得分:7)

Here是另一种基于将列宽度设置为零的解决方案。我修改了一下。它现在的工作原理如下:

  1. 使用bool-to-visibility转换器将标头的可见性绑定到ViewModel的布尔属性
  2. 在标题上使用附加属性将列的宽度设置为零
  3. 这是代码。

    XAML:

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

    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();
        }
    }
    

    附加行为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"
    }
    

答案 2 :(得分:0)

我已将列设置为宽度=&#34; 0&#34;为零。 现在该列看起来不可见。但我不知道它是否会影响其他任何事情。 它可能是一个虚拟的解决方案,但现在它可以工作。

答案 3 :(得分:0)

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

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

    set
    {
        OnPropertyChanged("ColumnWidth");
    }
}

进行绑定TwoWay并且如果用户尝试拖动更宽的列OnPropertyChanged将被调用并将宽度重置为0.您可能必须使用绑定代理来绑定。在更新ColumnIsVisible时,还要添加对OnPropertyChanged("ColumnWidth")的调用:)

答案 4 :(得分:-1)

使用Thumb.DragDelta可以解决问题

我在listview中使用它作为

<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;   
 }
}
}