I need to set the width of a columndefinition within a datatemplate for a listview based on some computation which is done in code behind. So I got this:
<DataTemplate x:Key="dataTemplateForListview">
<Border>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80" x:Name="gridColumnGraph" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
...
</Grid>
</Border>
</DataTemplate>
This Datatemplate is Bound to a ListView as the ItemTemplate. How do I get Access to “gridColumnGraph”? I need this access before the listview gets displayed – not when an Item is selected.
Thanks a lot!
Use Databinding to bind the Columndefinition.Width-Property to one of your Properties in code-behind or your ViewModel.
Make sure your ViewModel inherits from INotifiyPropertyChanged.
Create a method which calls the PropertyChanged-Event:
Create the property:
Bind to the property:
Set DataContext to ViewModel:
Change CDWidth in codebehind: