2012-09-25 85 views
2

此代码容易的方法是在我的应用程序变得越来越普遍:的标签添加到WPF ComboBox控件

<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。

我想让我成为一个自定义控件,它是一个ComboBox,它公开Label的内容,然后设置其他属性。东西可以像这样使用:

<Controls:CaptionedComboBox x:Name="comboBox" 
          Width="72" 
          LabelContent="_ComboBox Caption" 
          SelectedItem="{Binding ComboSelectedItem}" 
          ItemsSource="{Binding ComboSourceList}" /> 

不过,我看着做一个自定义的控制,并且存在需要定型的望而生畏量。

有没有一种方法来采取我上面的,并最终做这样的事情?我知道这是行不通的。但我的观点是,似乎很愚蠢,我不得不重新设计整个ComboBox的样式,只是为了在它上面添加一个标签。

+3

有你看着用户控件?所需的设计很少,只需设置DP属性即可。 –

+0

@HenkHolterman - 那里的问题(据我所知)是我必须重新公开组合框的所有属性。如果是这样,我想我只能选择两个邪恶中较小的一个。 – Vaccano

+0

这只是你没有提到所有选项。 ControlTemplate,UserCOntrol和CustomControl。我认为@塞巴斯蒂安的答案值得一试。 –

回答

5

您可以为一个模板,

<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> 

然后每当你想要使用标头的组合,只要使用

<ContentControl Template="{StaticResource ComboWithHeader}" DataContext="{Binding ComboBoxViewModel}" /> 
+0

这种方法可以使用它的两个实例(使用不同的标签)吗?看起来,这将只是一遍又一遍地制作相同的组合框/头组合。有没有办法在ContentControl级别绑定Label.Content,ComboBox.SelecteItem和ComboBox.ItemsSource? – Vaccano

+0

我假设你使用MVVM模式,ContentControl的datacontext将决定什么是项目源和组合框的选定项目。 标签内容也应该来自ViewModel。 ComboBoxViewModel的实例应该包含属性{ComboSourceList,ComboSelectedItem,ComboBoxHeader}。因此,您可以根据您传递的视图模型来区分控件中的项目,如DataContext –

+0

好的,这将起作用。这不是我想要的,但它会起作用。 (必须为我的ComboBoxHeader的每个实例创建一个单独的ViewModel有点冗长。) – Vaccano

0

很基本的,但那么简单哦,你也可以创建UserControl,然后将您的控件嵌入到您想要的任何位置。 例如

SimpleGraph

<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>