0

I've run out of ideas trying to collapse a DataGridTextcolumn depending on a value set in a public property (in a WPF/XAML/MVVM Light application).

An extract of the xaml is:

<StackPanel>
      <DataGrid AutoGenerateColumns="False" Name="PipelinesGrid" 
              HorizontalAlignment="Left" 
              ItemsSource="{Binding Pipelines}"
              SelectedItem="{Binding SelectedPipelineView, Mode=TwoWay}"  
              VerticalAlignment="Top" Margin="10,16,0,0" 
              SelectionUnit="FullRow" SelectionMode="Single" 
              CanUserAddRows="False">
        <DataGrid.Columns>
            <DataGridTextColumn 
                 Binding="{Binding DailyCapacity, Mode=TwoWay}" 
                 Header="Capacity(d)" 
                 Visibility="{Binding Source={x:Reference PipelinesGrid}, 
                                      Path=DataContext.ShowCapacities}"/>

Here is the Visibility setting is one of many that I've tried (this one gives a cycylical dependency error).

A relevant extract of the ViewModel code is:

private string _showCapacities;

public string ShowCapacities
{
    get { return _showCapacities; }
    set { _showCapacities = value; RaisePropertyChanged("ShowCapacities"); }
}

private void OnProjectSelected(ProjectSelectedArgs obj)
{
    _selectedProject = obj.SelectedProject;

    if (_selectedProject != null)
    {
        Pipelines = new ObservableCollection<PipelineView>();
        foreach (var projectPipeline in _selectedProject.ProjectPipelines)
        {
            Pipelines.Add(new PipelineView(projectPipeline));
        }

        switch (_selectedProject.ProjectCategory.ProjectCategory1)
        {
        case "Upstream":
            ShowCapacities = "Collapsed";
            break;
        case "Mid-Stream":
            ShowCapacities = "Visible";
            break;
        }
    }

    IsEditing = false;
}

I've been informed by this article (http://stackoverflow.com/questions/8847661/datagridtextcolumn-visibility-binding) that I needed to use Source and Xreference. Thanks for any help.

Phil
  • 42,255
  • 9
  • 100
  • 100

1 Answers1

1

There are various articles on SO that discuss this problem. Search for "wpf visibility binding datagrid column".

The problem is because DataGrid columns don't belong to the visual or logical tree of the DataGrid, so WPF can't find a suitable DataContext. Trying to use a relative or element binding fails as well.

Needless to say, some clever people have already solved this issue. My favourite solution is the proxy freezable method explained here: http://www.thomaslevesque.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherited/ which worked fine for me.

Phil
  • 42,255
  • 9
  • 100
  • 100
  • Thanks for your answer Phil, but I only noticed it to-day. I solved the problem in a different way before your reply. – James McDonnell Sep 13 '12 at 14:44
  • I'll try to reply again! Thanks Phil - I used two DataGrids and set their visibility bound to two opposite values held in properties and applied them in a switch statement in OnProjectSelected. – James McDonnell Sep 13 '12 at 15:09