0

I'm trying to figure out how to bind the viability of a DataGrid Template column to the observable collection that is the datacontext for the datagrid. Note: I'm a beginner with this stuff though, so you probably have to explain like you would to a 10yr old...

I know this is is related to this stackoverflow but I still can't seem to get it to go.

I have a breakpoint in the converter but it never gets hit when data is added to the observable collection...

I hope this makes sense... Thanks a lot

                    <DataGrid AutoGenerateColumns="False" AlternatingRowBackground="LightBlue" ItemsSource="{Binding historyColl}" Margin="0,75,6,26" Name="historyDataGrid" FontSize="12" HorizontalAlignment="Right" Width="381">
                    <DataGrid.Columns>
                        <DataGridTemplateColumn Header="Date (dd-mm)" Width="80*">
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding Date}" Background="{Binding Converter={StaticResource TradedBackground}}"/>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>

                        <DataGridTemplateColumn Header="Test" Width="50" Visibility="{Binding RelativeSource={x:Static RelativeSource.Self}, 
                                Path=FrameworkElement.DataContext, Converter={StaticResource booleanToVisiblityConverter}}">
                           <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding Test}"/>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>

                    </DataGrid.Columns>
                </DataGrid>
Community
  • 1
  • 1
keynesiancross
  • 3,441
  • 15
  • 47
  • 87
  • 1
    Didn't you forget to define your `booleanToVisiblityConverter` in resources? Also, I would use just `Visibility="{Binding PathToVisibilityFlagInDataContext, Converter={StaticResource booleanToVisiblityConverter}}"` with appropriate `PathToVisibilityFlagInDataContext`. (You seem to forget this one as well.) – Vlad Sep 17 '12 at 19:24
  • I did define the converter, but did it in the window resources though... where do I put this on the C# side though?PathToVisibilityFlagInDataContext – keynesiancross Sep 17 '12 at 19:29
  • I was basically hoping the converter would allow me to access the first row of the ObservableCollection, and then from there I could set the visibility.. – keynesiancross Sep 17 '12 at 19:31
  • Well, `PathToVisibilityFlagInDataContext` is not needed if your converter is ready to get the whole collection. – Vlad Sep 17 '12 at 19:42
  • Are you sure the data context of `DataGridTemplateColumn` is what you expect? I would try `RelativeSource={RelativeSource FindAncestor, AncestorType=DataGrid}`: `DataGrid` should have the data context as you expect. Did you try to check the data context with [snoop](http://snoopwpf.codeplex.com/)? – Vlad Sep 17 '12 at 19:45
  • Downloading snoop now, will give that a go. I added the above relative source bit but no dice.. – keynesiancross Sep 17 '12 at 19:49
  • so I have snoop going, and have it looking at my datagrid, but I have no idea how to find that column... – keynesiancross Sep 17 '12 at 19:56
  • Well, you can see the whole visual tree in snoop, right? You can expand the DataGrid node it and see which are the visual elements available. – Vlad Sep 18 '12 at 09:56

1 Answers1

0

I would join the discussion below the question but I don't have enough points to do so. I am pretty sure that Path=FrameworkElement.DataContext is the problem. The converter expects a boolean, but gets a DataContext instead.

I would expect a property of type bool within the DataContext itself to which you could bind the visibility, using the converter.

Defining the converter in window resources is just fine.

MrEdge
  • 74
  • 5