Quantcast
Channel: Caliburn.Micro: Xaml Made Easy
Viewing all articles
Browse latest Browse all 1760

Created Unassigned: Binding Data Grid column's header [333]

$
0
0
Hello!

I'm starting in using caliburn, and i'm loving it. I believe it is not an issue on the framework itself, but i'm not finding the way to solve this out.

So, here's the deal :

In WPF I was used to declare my view model as a Static Resource like this way:

```
<Window.Resources>
<viewModel:AlarmPanelViewModel x:Key="ViewModel"/>
<Window.Resources>
```

And bind my Column header this way :

```
<DataGrid ItemsSource="{Binding Items, Source={StaticResource ViewModel}}">

<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=ActivationDate}", Header="{Binding DateKey, Source={StaticResource ViewModel}}">
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>

```

So, my row values were going to be the ActivationDate's of my Items collection inside my View Model, and my Header would be my DateKey property, also in my view model.

Using caliburn, the ViewModel is dynamicaly instantiated and setted as the DataContext of my View. So my Data Grid endded up this way:

```
<DataGrid ItemsSource="{Binding Items}">

<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=ActivationDate}", Header="{Binding DateKey}"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>

```

The problem is, this way, my Header property seeks for the DateKey of the Item of the collection, not in my View Model. I would have to specify its data context by setting a relative source, so i tried:

```
<DataGridTextColumn Binding="{Binding Path=ActivationDate}" Header="{Binding DateKey, RelativeSource={RelativeSource FindAncestor, AncestorType=DataGrid}}" >
</DataGridTextColumn>
```

I've tried this way, and some variations, but the datacontext of my Header allways seems to be null.

Can anyone help me on how can I work this around? Thanks a lot!

Viewing all articles
Browse latest Browse all 1760

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>