Thursday, 5 September 2013

Can't refer to a Panel in the code-behind by it's name given in xaml

Can't refer to a Panel in the code-behind by it's name given in xaml

I have my own VirtualizingWrapPanel, that derives VirtualizingPanel and
IScrollInfo.
Here is the example of how it is used in xaml:
<ListBox x:Name="StationsListView"
DataContext="{StaticResource ViewModelKey}"
ItemsSource="{Binding Stations}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<common:VirtualizingWrapPanel IsItemsHost="True"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Button Style="{DynamicResource DestinationButtonStyle}">
<TextBlock Text="{Binding FullName}"
Style="{DynamicResource
DestinationStationTextBlockStyle}"
TextTrimming="CharacterEllipsis" />
</Button>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
If I give a name to the VirtualizingWrapPanel by x:Name="vwp" then I can
refer to the panel by it's name in a code-behind file, I mean,
IntelliSense offers to refer to the panel. But the compilation fails. The
compiler complains that it can't resolve the name I gave to the panel in
xaml.
I solved this issue by searching the panel in the object graph inside
ContendRendered event. But it seems like a dirty hack.
How can I overcome the issue by using the Name property (somehow)?

No comments:

Post a Comment