如何使用自定义模板在组合框中显示所选文本

时间:2014-06-09 13:56:59

标签: wpf combobox controltemplate

我是WPF的新手,实现了使用不同格式文本显示2个组合框的应用程序。

我为combobox创建了自定义controltemplate

<ControlTemplate x:Key="GridGenreComboboxTemplate" TargetType="{x:Type ComboBox}">
            <ControlTemplate.Resources>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
                <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent"/>
            </ControlTemplate.Resources>
            <StackPanel>
                <ToggleButton  
                                  IsTabStop="False" x:Name="DropDownToggle"
                                  HorizontalContentAlignment="Left" 
                                  IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}">

                    <ContentPresenter Content="{TemplateBinding SelectedItem}" 
                                  ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" 
                                  Margin="10,4" 
                                  VerticalAlignment="Center"
                                  HorizontalAlignment="Left">
                        <ContentPresenter.Resources>
                            <Style TargetType="{x:Type TextBlock}">
                                <Setter Property="Foreground" Value="{Binding Path=Foreground,RelativeSource={RelativeSource AncestorType={x:Type ComboBox}}}"/>

                            </Style>
                        </ContentPresenter.Resources>
                        <ContentPresenter.ContentTemplate>
                            <DataTemplate>
                                <TextBlock Text ="{Binding}"></TextBlock>
                            </DataTemplate>
                        </ContentPresenter.ContentTemplate>
                    </ContentPresenter>

                </ToggleButton>


                <!-- Popup for dropdown when combobox is clicked and open -->
                <Popup x:Name="PART_Popup" AllowsTransparency="True"
                           Width="{Binding Path=ActualWidth, RelativeSource={RelativeSource TemplatedParent}}"
                           Placement="Bottom" IsOpen="{Binding Path=IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}" 
                           PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}">
                    <Grid MaxHeight="{TemplateBinding MaxDropDownHeight}">
                        <Border x:Name="Splitter" BorderThickness="0,3,0,0">
                            <Border.BorderBrush>
                                <SolidColorBrush>
                                    <SolidColorBrush.Color>
                                        <Color R="58" G="64" B="70" A="255"/>
                                    </SolidColorBrush.Color>
                                </SolidColorBrush>
                            </Border.BorderBrush>
                        </Border>

                        <Grid Margin="0,3,0,0">
                            <Border CornerRadius="0,0,2,2" BorderThickness="1,0,1,1" x:Name="DropDownBorder"
                                Background="Green">
                                <Border.BorderBrush>
                                    <SolidColorBrush Opacity="0.9">
                                        <SolidColorBrush.Color>
                                            <Color R="96" G="96" B="97" A="255"/>
                                        </SolidColorBrush.Color>
                                    </SolidColorBrush>
                                </Border.BorderBrush>
                            </Border>

                            <ScrollViewer CanContentScroll="true" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch">
                                <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" x:Name="Content"
                                                    KeyboardNavigation.DirectionalNavigation="Continue"  HorizontalAlignment="Stretch"/>
                            </ScrollViewer>
                        </Grid>
                    </Grid>
                </Popup>
            </StackPanel>
        </ControlTemplate>

在一个组合框中想要显示格式化文本,即1000 x 900,200 x 300,...而在另一个想要显示 文本如1000 900拓扑,200 300拓扑

我已经格式化了组合框项目,它显示了正确的值,但选择的值没有显示在组合框上。 如何使用单个组合框项目模板?

    <ComboBox Grid.Row="0"   ItemsSource="{Binding PossibleTopologysNew}"
                         SelectedItem="{Binding SelectedTopologyNew}"             
                      Template="{StaticResource GridGenreComboboxTemplate}"  
                      >
            <ComboBox.ItemTemplate>
                <DataTemplate>
                        <TextBlock  Margin="10">
                            <TextBlock.Text>
                                <MultiBinding StringFormat="{}{0} x {1}">
                                    <Binding Path="LeftNumber"/>
                                    <Binding Path="RightNumber"/>
                                </MultiBinding>
                            </TextBlock.Text>
                        </TextBlock>

                    </DataTemplate>
            </ComboBox.ItemTemplate>
            </ComboBox>

 <ComboBox Grid.Row="0"   ItemsSource="{Binding PossibleTopologysNew}"
                         SelectedItem="{Binding SelectedTopologyNew}"             
                      Template="{StaticResource GridGenreComboboxTemplate}"  
                      >
            <ComboBox.ItemTemplate>
                <DataTemplate>
                        <TextBlock  Margin="10">
                            <TextBlock.Text>
                                <MultiBinding StringFormat="{}{0}  {1} topology">
                                    <Binding Path="LeftNumber"/>
                                    <Binding Path="RightNumber"/>
                                </MultiBinding>
                            </TextBlock.Text>
                        </TextBlock>

                    </DataTemplate>
            </ComboBox.ItemTemplate>
            </ComboBox>

1 个答案:

答案 0 :(得分:2)

您的修复很简单...只是ControlTemplate声明新ComboBox - 使用默认值。如果确实需要提供您自己的ControlTemplate,那么只需定义一个包含用于显示所选值的TextBox。如果您不熟悉WPF,那么我非常怀疑您确实需要定义自己的ControlTemplate

但是,如果你需要声明自己的ControlTemplate,那么你应该从默认的那个开始(可以在MSDN的ComboBox Styles and Templates页面找到)并慢慢小心地删除不需要的位,并添加您需要的新位。但是,在声明ControlTemplate时,您必须替换为like。例如,查看默认ContentPresenter中的ControlTemplate

<ContentPresenter x:Name="ContentSite"
    IsHitTestVisible="False"
    Content="{TemplateBinding SelectionBoxItem}"
    ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
    ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
    Margin="3,3,23,3"
    VerticalAlignment="Stretch"
    HorizontalAlignment="Left">

现在看看你的:

<ContentPresenter 
    Content="{TemplateBinding SelectedItem}" 
    ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" 
    Margin="10,4" 
    VerticalAlignment="Center"
    HorizontalAlignment="Left">

首先,您尝试将ContentPresenter.Content属性数据绑定到与原始ControlTemplate不同的属性。所以,如果我是你,我会摆脱你的不工作ControlTemplate并将其替换为原来的。确保在开始更改之前正确显示,并在继续更改时定期测试。