tags:

views:

618

answers:

4

I have a list view like

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

I use 2 buttons. When click on first button the Col3 should collapse and it should be visible when a click in the second button.

Any idea to do such a listview in WPF?

A: 

Could you provide some xaml-code of what your listview looks like?

You could bind a RelayCommand to your button and pass the ListView as a parameter. Then you could set Visibility = False.

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

This would be your 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;
}

I'm talking about RelayCommand as in Josh Smith's example: http://msdn.microsoft.com/en-us/magazine/dd419663.aspx
you can dl the code there.

I guess you could achieve a similar result in xaml only with triggers, however I'm not as experienced with that subject.

Torsten
Which namespace contain RelayCommand?
Sauron
You have to create your own RelayCommand class. DL the code from the link I posted above (or take this quick link: http://code.msdn.microsoft.com/mag200902MVVM/Release/ProjectReleases.aspx?ReleaseId=2026). "RelayCommand is a simplified variation of the DelegateCommand"
Torsten
A: 

I am using the code as

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

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

Can u give me a better idea?

Sauron
A: 

I'd have put this answer as a comment of your post, but I'm not able to comment yet, so...

You have to name (use the "Name" Property) the element that you want to access via "Binding ElementName" or you won't be able to get it. In your case you should explicitly create the GridViewColumnHeader, because GridViewColumn has no Visibilty property:

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

You probably also have to explicitly create the content of your GridViewColumn if you want to make it disappear, though. This means you have to use GridViewColumn.DisplayMemberBinding or GridViewColumn.CellTemplate and give those a Name as well or access them via RelativeSource.

Have a look at this for the possibilities: http://www.wpfwiki.com/Default.aspx?Page=WPF%20Q5.3&amp;AspxAutoDetectCookieSupport=1
However, have you thought about using an expander, yet?

Torsten
No Name property available for GridViewColumnHeader.
Sauron
Where have you got that information from? If I use intellisense or simply look at the msdn library I can find the Name property: http://msdn.microsoft.com/en-us/library/system.windows.controls.gridviewcolumnheader_members.aspxAre you using a .NET version older than 3.5?
Torsten
another thing that comes to my mind is the namespace xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml". Usually it's added by default, but maybe you have to use that one. Then you could use x:Name as a property.
Torsten
+1  A: 

Use of Thumb will solve the problem. Just 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;   
   }
}
}
Sauron