以编程方式隐藏listview标头

时间:2009-05-14 08:55:29

标签: c# wpf listview

我有ListView喜欢:

Col1  Col2  Col3
1      A     I
2      B     II
3      C     III 

我使用2个按钮。当我点击第一个按钮时,Col3应该折叠,当第二个按钮点击时它应该是可见的。

有关如何在WPF中执行此类ListView的任何想法吗?

4 个答案:

答案 0 :(得分:1)

使用Thumb可以解决问题。 就像

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

答案 1 :(得分:0)

你能提供一些你的listview看起来像什么的xaml代码吗?

您可以将RelayCommand绑定到按钮并将ListView作为参数传递。然后你可以设置Visibility = False。

<Button Command="{Binding MyButtonCommand}
    CommandParameter="{Binding ElementName=Col3}" />

这将是你的cs:

ICommand _myButtonCommand;
public ICommand MyButtonCommand
{
    get
    {
        if (_myButtonCommand== null) _myButtonCommand= new RelayCommand(param => HideList(param ));
        return _myButtonCommand;
    }
}

void HideList(object param){
    (param as ListView).Visibility = False;
}

我在谈论RelayCommand和Josh Smith的例子:http://msdn.microsoft.com/en-us/magazine/dd419663.aspx
你可以在那里找到代码。

我猜你只能通过触发器在xaml中获得类似的结果,但是我对这个主题并不熟悉。

答案 2 :(得分:0)

我正在使用代码

<Grid>
    <ListView HorizontalContentAlignment="Stretch" Margin="38,12,31,110">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="COL1" Width="100"/>
                <GridViewColumn Header="COL2" Width="100"/>
                <GridViewColumn Header="COL3" Width="100"/>
            </GridView>
        </ListView.View>
    </ListView>
    <Button Height="25" HorizontalAlignment="Left" Margin="105,0,0,51" 
            Name="Collapse" VerticalAlignment="Bottom" Width="75"
            Command="{Binding MyButtonCommand}"    
            CommandParameter="{Binding ElementName = COL3}">Collapse</Button>
    <Button Height="25" HorizontalAlignment="Right" Margin="0,0,111,51" Name="Expand" 
            VerticalAlignment="Bottom" Width="75">Expand</Button>
</Grid>

和CS

 ICommand _myButtonCommand;
    public ICommand MyButtonCommand
    {
        get 
        { 
            if (_myButtonCommand== null) _myButtonCommand = new RelayCommand(param => HideList(param ));  
            return _myButtonCommand;
        }
    }
    void HideList( object param )
    {
        ( param as ListView ).Visibility = Visibility.Hidden;
    }
你可以给我一个更好的主意吗?

答案 3 :(得分:0)

我已经把这个答案作为你的帖子的评论,但我还没有评论,所以......

您必须通过“Binding ElementName”命名(使用“Name”属性)您要访问的元素,否则您将无法获取它。在您的情况下,您应该显式创建GridViewColumnHeader,因为GridViewColumn没有Visibilty属性:

<GridViewColumnHeader Name="COL3">COL3</GridViewColumnHeader>

如果你想让它消失,你可能还必须明确地创建GridViewColumn的内容。这意味着您必须使用GridViewColumn.DisplayMemberBinding或GridViewColumn.CellTemplate并为其提供名称或通过RelativeSource访问它们。

看看这个可能性:http://www.wpfwiki.com/Default.aspx?Page=WPF%20Q5.3&AspxAutoDetectCookieSupport=1
但是,您是否考虑过使用扩展器?

相关问题