此代码在我的应用程序中变得越来越常见:
<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>
我知道那是行不通的。但我的观点是,我必须重新设置整个 ComboBox 的样式只是为了在它上面添加一个标签,这似乎很愚蠢。