这段代码在我的应用程序中越来越常见:
<StackPanel Orientation="Vertical">
<Label Content="_ComboBox Caption"
Target="comboBox"
Margin="0"
Padding="5,5,5,1" />
<ComboBox x:Name="comboBox"
Width="72"
Margin="5,0,5,5"
Padding="5,1,5,5"
SelectedItem="{Binding ComboSelectedItem}"
ItemsSource="{Binding ComboSourceList}" />
</StackPanel>
它可以为我渲染一个带标题的组合框。
我想要创建一个自定义控件,即 ComboBox,它可以公开标签内容并设置其他属性。如下所示:
<Controls:CaptionedComboBox x:Name="comboBox"
Width="72"
LabelContent="_ComboBox Caption"
SelectedItem="{Binding ComboSelectedItem}"
ItemsSource="{Binding ComboSourceList}" />
然而,我研究了一下如何制作自定义控件,需要大量的样式设计。令人望而生畏。
有没有办法将上述内容做成像这样的效果?
<StackPanel Orientation="Vertical">
<Label Content="{Binding TemplateLabelContent}"
Target="{Binding ControlName}"
Margin="0"
Padding="5,5,5,1" />
<InheritedComboBoxStuff/>
</StackPanel>
我知道那样做行不通。但我的观点是,如果只是为了在组合框上方添加标签就必须重新设计整个组合框,那么这似乎很荒谬。