[英]Add a StackPanel to a ComboBoxItem Programmatically
我有一个基于WPF构建的Windows程序。 我正在利用ini文件使它更适合最终用户定制。 目前,我有一个包含多个项目的ComboBox,并且在ComboBox的各个部分之间有一个StackPanel:
这是XAML:
<ComboBox Grid.ColumnSpan="2" Grid.Column="0" HorizontalAlignment="Stretch" Margin="75,0,5,5" Grid.Row="1" VerticalAlignment="Top">
<ComboBoxItem IsEnabled="False">
<StackPanel>
<TextBlock Text="50% Margin" />
<Rectangle />
</StackPanel>
</ComboBoxItem>
<ComboBoxItem Content="Windows" Uid="0" />
<ComboBoxItem Content="Gutters" Uid="0" />
<ComboBoxItem Content="Insulation" Uid="0" />
<ComboBoxItem Content="Doors" Uid="0" />
<ComboBoxItem IsEnabled="False">
<StackPanel>
<TextBlock Text="50% Margin, No Overage" />
<Rectangle />
</StackPanel>
</ComboBoxItem>
<ComboBoxItem Content="Kitchens" Uid="1" />
<ComboBoxItem Content="Bathrooms" Uid="1" />
<ComboBoxItem Content="Additions" Uid="1" />
<ComboBoxItem Content="Basements" Uid="1" />
<ComboBoxItem IsEnabled="False">
<StackPanel>
<TextBlock Text="35% Margin" />
<Rectangle />
</StackPanel>
</ComboBoxItem>
<ComboBoxItem Content="Siding" Uid="2" />
<ComboBoxItem Content="Roofing" Uid="2" />
</ComboBox>
如何以编程方式在ComboBox项内添加StackPanel? 对我来说不幸的是,ComboBoxItem没有child属性,这使这真的很容易。
ComboBoxItem
具有应执行所需操作的Content
属性。
通常,如果您想知道元素的XAML内容映射到哪个属性,请沿继承层次结构查找[ContentPropertyAttribute]
属性。
ComboBoxItem
继承自ListBoxItem
,而ListBoxItem
本身又继承自ContentControl
,后者通过[ContentPropertyAttribute("Content")]
标记。 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.