1

I'm currently facing a problem while trying to do some conditional binding in WPF. I've read up on the problem and it seems like "visibility" isn't really an option for DataGridColumns as it's not in the logicaltreeview. I currently have an object "Device" which contains a list of objects "Channel". These channels can be either input or output which is represented as a bool "isInput". What I'm trying to accomplish is to create two data grids, one with inputs and one with outputs.

<DataGrid Grid.Row="0" AutoGenerateColumns="False" ItemsSource="{Binding Path=Channels}">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Path=Type}" 
             Visibility="{Binding Path=(model:Channel.IsInput), 
             Converter={StaticResource BooltoVisibilityConverter}}"/>
        </DataGrid.Columns>
</DataGrid>

This is what I currently have but as the visibility doesn't seem to work I would like a way to either hide the whole row when IsInput=false or to skip it entirely.

1 Answers1

3

If you want multiple grids, then you need multiple item collection filtered as required.

For what you require, assuming that the total number of channel objects is relatively small, I'd do something like this.

public class ViewModel: ViewModelBase
{
    public ViewModel()
    {
        AllChannels = new ObservableCollection<Channel>();
        AllChannels.CollectionChanged += (s,e) =>
           { 
               RaisePropertyChanged(nameof(InputChannels));
               RaisePropertyChanged(nameof(OutputChannels));
           }
    }

    private ObservableCollection<Channel> AllChanels { get; }

    public IEnumerable<Channel> InputChannels => AllChannels.Where(c => c.IsInput);
    public IEnumerable<Channel> OutputChannels => AllChannels.Where(c => !c.IsInput);

    public void AddChannel(Channel channel)
    {
        AllChannels.Add(channel);
    }
}        

You can now create two grid controls and bind their ItemsSource property to InputChannels and OutputChannels.

Peregrine
  • 4,287
  • 3
  • 17
  • 34
  • Thank you. Was wondering if you could avoid having to create multiple lists but i guess this is the way to do it :) – Stephan Fuhlendorff Oct 31 '18 at 11:51
  • @StephanFuhlendorff Even if you're not going to go the whole MVVM route, it's usually cleaner and easier to separate the U.I. from any kind of data logic. – Peregrine Oct 31 '18 at 12:02
  • Of course. I was just afraid it would cause problems later on when trying to edit the channels so i was trying to keep them together for as long as possible :) – Stephan Fuhlendorff Oct 31 '18 at 12:08