【问题标题】:WPF - Formatting a ComboBox's displayed itemWPF - 格式化组合框的显示项
【发布时间】:2018-02-02 20:44:41
【问题描述】:

我已经格式化了一个 ComboBox 来显示每个项目的详细信息,请不要将设计视为最终设计,这只是一个示例:

但是如你所见,显示的项目(带箭头的框内)坏了:

所以我也需要格式化该组件,以便在该框中仅显示服务器值。我试图找到正确的元素,甚至设法找到了重新格式化整个组合框的方法,但没有办法为该框内的数据显示添加模板。

如何编辑该容器的数据模板?这是我期望的结果:

<ComboBox x:Name="cboSourceMySql" Grid.Column="1" Margin="5,0,5,0" ItemsSource="{Binding OdbcDataSources, Mode=TwoWay}" Grid.Row="1" >

    <ComboBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="50" />
                    <ColumnDefinition Width="1*" />
                    <ColumnDefinition Width="50" />
                    <ColumnDefinition Width="1*" />
                </Grid.ColumnDefinitions>
                <Label Content="Server:" Grid.Column="0" Grid.Row="0" />
                <TextBlock Text="{Binding Server}" VerticalAlignment="Center" Grid.Column="1" Grid.Row="0" />
                <Label Content="Driver:" Grid.Column="0" Grid.Row="1" />
                <TextBlock Text="{Binding Driver}" VerticalAlignment="Center" Grid.Column="1" Grid.Row="1" />
            </Grid>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

【问题讨论】:

  • 请编辑您的问题并发布代码。
  • @aduguid 我为当前的 ComboBox 添加了代码 sn-p

标签: c# wpf combobox datatemplate


【解决方案1】:

来自文章WPF Combobox: Different template in textbox and drop-downlist 我认为您可以使用以下样式

  <ComboBox 
     x:Name="cboSourceMySql" 
       Grid.Column="1" Margin="5,0,5,0" 
       ItemsSource="{Binding OdbcDataSources, Mode=TwoWay}" 
       Grid.Row="1" >
            <ItemsControl.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Server}" />
            </DataTemplate>
        </ItemsControl.ItemTemplate>
        <ComboBox.ItemContainerStyle>
            <Style TargetType="{x:Type ComboBoxItem}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ComboBoxItem}">
                            <Border
                                x:Name="Bd"
                                Background="{TemplateBinding Background}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}">
                                <StackPanel Orientation="Vertical">
                                    <Label Content="{Binding Server}" />
                                    <Label Content="{Binding Driver}" />
                                </StackPanel>
                            </Border>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsHighlighted" Value="True">
                                    <Setter TargetName="Bd" Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
                                    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}" />
                                </Trigger>
                                <Trigger Property="IsEnabled" Value="False">
                                    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ComboBox.ItemContainerStyle>


    </ComboBox>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-21
    • 1970-01-01
    • 2013-03-04
    • 2010-12-07
    相关资源
    最近更新 更多