我在/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