我在/Themes/Generic.xaml以下的ControlTemplate:控件模板背后动态地添加到一个StackPanel
<ControlTemplate x:Key="GroupList" TargetType="{x:Type Expander}">
<Expander IsExpanded="True">
<Expander.Header>
<Label Style="{DynamicResource headline3}" FontWeight="Light" BorderThickness="0 0 0 1">TEst group</Label>
</Expander.Header>
<ListBox x:Name="lstBoxContacts" Height="auto" BorderThickness="0" VerticalAlignment="Top" AllowDrop="True" PreviewDragOver="lstBoxContacts_PreviewDragOver" Drop="lstBoxContacts_Drop" ItemsSource="{Binding Contacts}">
<ListBox.ItemTemplate>
<DataTemplate>
<Label Content="{Binding Fullname}" />
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ContextMenu>
<ContextMenu >
<MenuItem Header="Send Message" Command="local:CustomCommands.cmdMessageWrite"/>
<MenuItem Header="Show Details" Command="local:CustomCommands.cmdContactDetails"/>
<MenuItem Header="Delete" Command="local:CustomCommands.cmdContactDelete"/>
</ContextMenu>
</ListBox.ContextMenu>
</ListBox>
</Expander>
</ControlTemplate>
这应该给我用一个ListBox的膨胀。我可以通过一个ObservableCollection<User>
对象,而不是显示(只有用户的全名属性)。
在我的MainWindow.xaml中,我有一个名为“stkpContactsAndGroups”的Stackpanel,我想以编程方式和动态方式添加Expanders。
内容来自API调用(其中所有的作品)。
从我所看到的我应该使用Template.FindName。但是,当我提供可以找到所述模板的来源VS VS bucks。
Expander grp = (Expander)Template.FindName("GroupList", new Generic());
stkpContactsAndGroups.Children.Add(grp);
它无法将Generic()转换为FrameworkElement。我在这里错过了点吗?
难道你不想将'Custom Control'添加到' StackPanel'而不是'ControlTemplate'(自定义控件的) – SWilko
@SWilko我对WPF不太好,我不确定是否完全理解所述操作的后果。我认为最简单的解决方案是拥有一个控件的模板,我可以添加数据并将其添加到我的StackPanel。 – Richard
Generic in a'new Generic()'ResourceDictionary declared /Themes/Generic.xaml? – StepUp