My second problem is a bit more mysterious. As I said, I'm trying to spawn controls from my application code. One of my goals is to fill a StackPanel with a custom control containing a custom drop down menu. But as soon as I try to add one in the Panel (using UIElementCollection::Add), the application crashes. Here are some code snippets:
test.xaml, loaded by the application:
<Border
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
HorizontalAlignment="Center" VerticalAlignment="Center"
x:Name="PART_BorderTest"
UseLayoutRounding="True">
<FilterBar x:Name="PART_FilterBar" />
</Border>
filterBar.xaml
<FilterBar
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="FilterBar" x:Name="FilterBar"
UseLayoutRounding="True">
<StackPanel x:Name="PART_CategoryPanel" Orientation="Horizontal" />
</FilterBar>
filterCategory.xaml
<FilterCategory
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="FilterCategory" x:Name="FilterCategory"
UseLayoutRounding="True">
<FilterCategory.Resources>
<ControlTemplate x:Key="MenuAnchorTemplate" TargetType="{x:Type MenuAnchor}">
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<ContentPresenter x:Name="PART_HeaderContent"
Content="{Binding HeaderContent, RelativeSource={RelativeSource TemplatedParent}}"/>
<ToggleButton IsChecked="{Binding IsMenuOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"/>
</StackPanel>
<Popup x:Name="PART_Popup"
IsOpen="{Binding IsMenuOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
Focusable="False"
PlacementTarget="{Binding ElementName=PART_HeaderContent}"
Placement="Bottom"
PopupAnimation="Slide"
VerticalOffset="1"
AllowsTransparency="True"
StaysOpen="True">
<ContentPresenter/>
</Popup>
</StackPanel>
</ControlTemplate>
<Style TargetType="{x:Type MenuAnchor}">
<Setter Property="Template" Value="{StaticResource MenuAnchorTemplate}"/>
</Style>
</FilterCategory.Resources>
<MenuAnchor>
<MenuAnchor.HeaderContent>
<StackPanel Orientation="Horizontal">
<CheckBox/>
<TextBlock Text="{Binding CategoryID, Mode=TwoWay, StringFormat=F2}" />
</StackPanel>
</MenuAnchor.HeaderContent>
<StackPanel x:Name="PART_FilterPanel" Orientation="Vertical"/>
</MenuAnchor>
</FilterCategory>
Note that I've encountered both problems using that ControlTemplate for MenuAnchor (the custom drop down menu) shown in filterCategory.xaml.
Add function
Noesis::Gui::UIElementCollection* pCol = _filtersCategoriesPanel->GetChildren();
if (pCol != nullptr)
{
pCol->Add(pNewCategory);
}
I've been looking at this code and I think there is something wrong with the design.
Custom Controls don't have a xaml associated with the class, so when you reference a custom control from another xaml like in test.xaml, you cannot expect that FilterBar instance will contain a StackPanel named "PART_CategoryPanel" set as content. PART_ elements must be defined in the control template in the same way you did with the MenuAnchorTemplate.
If you need to associate a xaml with a control, then you need to create a
UserControl, and override the UserControl.Source property to specify which xaml file must be loaded when your user control gets created.
I tried to add some custom controls to a panel without problem so my guess is that the crash you were getting was because the _filtersCategoriesPanel was null (maybe you were assuming that filterCategory.xaml would be loaded and PART_FilterPanel element could be found). Could that be possible?