向 WPF ComboBox 控件添加标签的简单方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12590449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Easy way to add a Label to the WPF ComboBox control
提问by Vaccano
This code is becoming more common in my application:
此代码在我的应用程序中变得越来越普遍:
<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>
It renders me a Captioned ComboBox.
它呈现给我一个 Captioned ComboBox。
I would like to make me a custom control that is a ComboBox that exposes the Label's content and then sets the other properties up. Something that could be used like this:
我想让我成为一个自定义控件,它是一个 ComboBox,它公开 Label 的内容,然后设置其他属性。可以这样使用的东西:
<Controls:CaptionedComboBox x:Name="comboBox"
Width="72"
LabelContent="_ComboBox Caption"
SelectedItem="{Binding ComboSelectedItem}"
ItemsSource="{Binding ComboSourceList}" />
However, I looked into making a custom control and there is a dauntingamount of styling that is needed.
然而,我看着做一个自定义的控制,有一个艰巨的是需要造型的量。
Is there a way to take what I have above and end up doing something like this?
有没有办法接受我上面的内容并最终做这样的事情?
<StackPanel Orientation="Vertical">
<Label Content="{Binding TemplateLabelContent}"
Target="{Binding ControlName}"
Margin="0"
Padding="5,5,5,1" />
<InheritedComboBoxStuff/>
</StackPanel>
I know that will not work. But my point is that is seems silly that I have to re-style the whole ComboBox just to add a label above it.
我知道这行不通。但我的观点是,我必须重新设计整个 ComboBox 的样式只是为了在它上面添加一个标签,这似乎很愚蠢。
回答by Sebastian Dymel
You can make a template for that
你可以为此制作一个模板
<ControlTemplate x:Key="ComboWithHeader" TargetType="ContentControl">
<StackPanel Orientation="Vertical">
<Label Margin="0"
Content="{Binding ComboBoxHeader}"
DataContext="{TemplateBinding DataContext}"
Padding="5,5,5,1"
Target="comboBox" />
<ComboBox x:Name="comboBox"
Width="72"
Margin="5,0,5,5"
DataContext="{TemplateBinding DataContext}"
ItemsSource="{Binding ComboSourceList}"
Padding="5,1,5,5"
SelectedItem="{Binding ComboSelectedItem}" />
</StackPanel>
</ControlTemplate>
And then whenever you want to use combo with header, just use
然后每当你想使用带有标题的组合时,只需使用
<ContentControl Template="{StaticResource ComboWithHeader}" DataContext="{Binding ComboBoxViewModel}" />
回答by AlSki
Really basic but oh so simple, you can also create a UserControl, and then embed your control anywhere you want to as well. e.g.
非常基本但非常简单,您还可以创建一个 UserControl,然后将您的控件嵌入到您想要的任何位置。例如


<UserControl x:Class="xxx.View.TestResultsGraph"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d"
MinHeight="50"
d:DesignHeight="50" d:DesignWidth="300" >
<Canvas Name="canvas" >
<Polygon x:Name="SuccessShape" Fill="#FFF0FFF0" SnapsToDevicePixels="True" />
<Polyline x:Name="SuccessLine" Stroke="Green" StrokeThickness="0.5" SnapsToDevicePixels="True" />
<Polygon x:Name="FailShape" Fill="#FFFFF0F0" SnapsToDevicePixels="True" />
<Polyline x:Name="FailLine" Stroke="Red" StrokeThickness="0.5" SnapsToDevicePixels="True" />
<Polygon x:Name="PendingShape" Fill="#FFFFF0E0" SnapsToDevicePixels="True" />
<Polyline x:Name="PendingLine" Stroke="DarkOrange" StrokeThickness="0.5" SnapsToDevicePixels="True" />
<Line x:Name="xAxis" Stroke="Black" StrokeThickness="0.5" SnapsToDevicePixels="True" />
<Line x:Name="yAxis" Stroke="Black" StrokeThickness="0.5" SnapsToDevicePixels="True" />
</Canvas>

