ContentPresenter DataContext bug
Posted: 13 Dec 2024, 14:11
hi noesis team, please help me with my problem
I want to create custom control like main screen on android/iphone device. So I've created grid with several pages and inside each page I've placed several buttons. Also I've created custom component Swiper to swipe pages like on main screen of android or ios system.
So my hierarcy is: SWIPER (collection of pages) -> ItemsControl (one page, collection of buttons) -> Button ( one simple button with icon and text).
And I've created such view models:
using Page = Noesis::ObservableCollection<TabletGridButtonVM>;
Noesis::Ptr<ObservableCollection<Page>> pages;
also I've added such style for swiper
when I open GUI Inspector, I've found that ContentPresenter doesn't have DataContext (check 1st image)
when I'm adding manually binding for DataContext field <Grid x:Name="PageGrid" DataContext="{Binding DataContext, RelativeSource={RelativeSource AncestorType=UserControl}}">, DataContext was passed deeper but in the ContentPresenter in ItemsControl it is also wrong (check 2nd image)
please help me what I'm doing wrong ?
I want to create custom control like main screen on android/iphone device. So I've created grid with several pages and inside each page I've placed several buttons. Also I've created custom component Swiper to swipe pages like on main screen of android or ios system.
So my hierarcy is: SWIPER (collection of pages) -> ItemsControl (one page, collection of buttons) -> Button ( one simple button with icon and text).
And I've created such view models:
using Page = Noesis::ObservableCollection<TabletGridButtonVM>;
Noesis::Ptr<ObservableCollection<Page>> pages;
Code: Select all
<br:Swiper
Margin="0,0,0,0"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Grid.Row="1" PagesSource="{Binding Pages}">
<br:Swiper.PageDataTemplate>
<DataTemplate>
<Grid x:Name="PageGrid" DataContext="{Binding DataContext, RelativeSource={RelativeSource AncestorType=UserControl}}">
<ItemsControl Margin="40,40,40,40" Background="#00000000" ItemsSource="{Binding}" >
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="5" Rows="3"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid >
<TextBlock Text="{Binding Text}" FontSize="24" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,152,0,0" Foreground="#FFFFFFFF"/>
<Button Background="#00000000" Foreground="#00141414" BorderBrush="#00000000">
<Grid Width="200" Height="200" HorizontalAlignment="Center" VerticalAlignment="Center" Background="#00000000">
<Image Source="{Binding Icon}" Width="116" Height="116" Margin="0,0,0,32"/>
<TextBlock Text="{Binding Text}" FontSize="24" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,152,0,0" Foreground="#FFFFFFFF"/>
<Rectangle Visibility="{Binding HasBadge, Converter={BooleanToVisibilityConverter}}" RadiusX="11" RadiusY="11" VerticalAlignment="Center" HorizontalAlignment="Center" Height="24" Width="24" Fill="#E2310C" Margin="0,-132,-104,0"/>
</Grid>
</Button>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
</DataTemplate>
</br:Swiper.PageDataTemplate>
</br:Swiper>
Code: Select all
<!-- Swiper -->
<DataTemplate x:Key="Template.SwiperDot">
<Ellipse Height="10" Width="10" Fill="{DynamicResource Brush.SwiperDot}" Margin="7 0 0 0"/>
</DataTemplate>
<DataTemplate x:Key="Template.SwiperDot.Selected">
<Ellipse Height="10" Width="10" Fill="{DynamicResource Brush.SwiperDot.Selected}" Margin="7 0 0 0"/>
</DataTemplate>
<ControlTemplate x:Key="Template.Swiper" TargetType="{x:Type br:Swiper}">
<Grid ClipToBounds="True">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="20"/>
</Grid.RowDefinitions>
<ContentPresenter
Grid.Row="0"
x:Name="SWIPER_content" DataContext="{TemplateBinding}">
</ContentPresenter>
<StackPanel
x:Name="SWIPER_pagination"
Grid.Row="1"
Orientation="Horizontal"
HorizontalAlignment="Center"
Margin="0 7 0 0">
</StackPanel>
</Grid>
</ControlTemplate>
<Style TargetType="{x:Type br:Swiper}">
<Setter Property="br:Swiper.Template" Value="{DynamicResource Template.Swiper}"/>
<Setter Property="br:Swiper.DotDataTemplate" Value="{DynamicResource Template.SwiperDot}"/>
<Setter Property="br:Swiper.CurrentDotDataTemplate" Value="{DynamicResource Template.SwiperDot.Selected}"/>
</Style>