3

此代码在我的应用程序中变得越来越常见:

<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 的样式只是为了在它上面添加一个标签,这似乎很愚蠢。

4

2 回答 2

7

你可以为此制作一个模板

  <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}" />
于 2012-09-25T20:38:52.040 回答
-1

非常基本但非常简单,您还可以创建一个用户控件,然后将控件嵌入到您想要的任何位置。例如

简单图

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

于 2012-09-25T21:07:10.640 回答