【问题标题】:how to show selected text in combobox using custom template如何使用自定义模板在组合框中显示选定的文本
【发布时间】:2014-06-09 13:56:59
【问题描述】:

我是 WPF 新手,正在实现应用程序以显示 2 个具有不同格式文本的组合框。

我已经为组合框创建了自定义控件模板

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

【问题讨论】:

    标签: wpf combobox controltemplate


    【解决方案1】:

    您的解决方法很简单......只是不要ComboBox 声明一个新的ControlTemplate - 使用默认的。如果你真的需要提供你自己的ControlTemplate,那么只需定义一个包含TextBoxTextBox 用于显示所选值。如果您是 WPF 新手,那么我非常怀疑您是否真的需要定义自己的 ControlTemplate

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

    <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 并用原来的替换它。在开始更改之前确保它显示正确,并在继续更改时定期对其进行测试。

    【讨论】:

    • 默认组合框正在工作。但是想用自定义控件。只要。我尝试了您更新的内容,但它无法正常工作,请您在最后检查一下
    • 我并不是建议您的解决方案只是替换我向您指出的那个示例。我是说当你想创建自己的ControlTemplate 时,你应该从默认的开始,慢慢地、仔细地进行更改,并在此过程中定期对其进行测试。所以你说默认的工作。现在你只需要一点一点地改变它,直到它满足你的要求。
    • 通过添加 ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" 解决了这个问题。非常感谢,我从过去 4 天开始尝试解决此问题,而您在半小时内解决了它。非常感谢您的帮助。
    • 我有另一个查询,上面的模板在我们的项目中很常见,它没有设置 ContentTemplate。但是对于我的组合框,我需要设置 ContentTemplate,在这种情况下,我是否需要为我的组合框复制相同的自定义控件代码并添加 contentTemplate,或者有没有办法在我的组合框代码中使用相同的控件模板代码并设置内容模板。?
    • 我不确定我是否完全理解您的问题。 ContentTemplateContentControl 和/或 ContentPresenter 类的属性。他们和你的ComboBox 有什么关系?无论哪种方式,如果您询问如何将此ContentTemplate 应用于您的其他控件,那么您可以在Application.Resources 没有x:Key 中声明它。有关这方面的更多信息,请参阅 MSDN 上 Data Templating Overview 页面的“数据类型属性”部分。
    猜你喜欢
    • 2018-05-17
    • 2021-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多