0
votes

I created an attached property for a GridView's GridViewColumn to hide/show the whole column.

    <GridViewColumn att:Visibility.IsVisible="{Binding ...}">

This works as long the property the attached property binds to exists in my main view model (the datacontext of my gridView). In one case, the visibility of the column depends on a property of my list item (an item inside the collection that is the itemsource of my gridview). How do I bind here? I can access the item properties inside the cell template of the gridviewcolumn but not in the gridviewcolumn itself.

What I'm trying to achieve is, that I can bind the attached property to a property of my list item. Is there a way to do this?

Edit: Let's say I have a class:

public class Item : INotifyPropertyChanged
{
  private string m_Text;
    public string Text
    {
        get
        {
            return m_Text;
        }
        set
        {
            m_Text= value;
            RaisePropertyChanged("Text");
        }
    }
    private bool m_IsVisible;
    public bool IsVisible
    {
        get
        {
            return m_IsVisible;
        }
        set
        {
            m_IsVisible = value;
            RaisePropertyChanged("IsVisible");
        }
    }
}

Now I have an ObservableCollection of this class as ItemsSource of my ListView/GridView.

public ObservableCollection<Item> Items;

In my xaml:

<ListView ItemsSource={Binding Items}>
 <ListView.View>
  <GridView>
    <GridView.Columns>
     <GridViewColumn att:Visibility.IsVisible="{Binding /*What goes here? Should bind to IsVisible of my Collection Item*/}">
      <GridViewColumn.CellTemplate>
       <DataTemplate>
        <TextBlock Text={Binding Text"/>    
        </DataTemplate>
      </GridViewColumn.CellTemplate>
     </GridViewColumn>
    </GridView.Columns>
  </GridView>
 </ListView.View>
</ListView/>

The visibility of my gridviewcolumn should now depend on the value of "IsVisible" of my collection item. Is that event possible?

1
post, please, your list property - StepUp

1 Answers

0
votes

You can bind your attached property to the IsVisible property of your collection. I noticed that your xaml syntax is incorrect cause compilation errors. Your xaml should look like this.

 <ListView ItemsSource="{Binding Items}">
        <ListView.View>
            <GridView>
                <GridView.Columns>
                    <GridViewColumn att:Visibility.IsVisible="{Binding IsVisible}">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Text}"/>    
                            </DataTemplate>                              
                        </GridViewColumn.CellTemplate>                            
                    </GridViewColumn>                       
                </GridView.Columns>
            </GridView>
        </ListView.View>
    </ListView>

Also you don't need to implement INotifyPropertyChanged on your Item class. Your ObservableCollection already implements INotifyCollectionChanged to it already will update the UI. Your Item class should just like like this

 public class Item
{
    public string Text { get; set; }
    public bool IsVisible { get; set; }
}