WPF:ComboBoxItem背景

时间:2009-05-03 15:39:28

标签: wpf combobox

我有一个基于XML文件数据的ComboBox:

<Root>
<Node Background="Yellow" Foreground="Cyan" Image="1.ico" Property="aaaa" Value="28" />
<Node Background="SlateBlue" Foreground="Black" Image="2.ico" Property="bbbb" Value="2.5" />
<Node Background="Teal" Foreground="Green" Image="3.ico" Property="cccc" Value="4.0" />
<Node Background="Yellow" Foreground="Red" Image="4.ico" Property="dddd" Value="0" /></Root>

因此,在这种情况下,我需要在每个项目都有合适的背景时创建一个复合ComboBoxItem。

我试着这样做:

    <UserControl.Resources>
    <DataTemplate DataType="Node">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" MinWidth="20"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="Auto" MinWidth="20"/>
            </Grid.ColumnDefinitions>
            <Border Background="{Binding XPath=@Background}" Grid.Column="0">
                <Image Source="{Binding XPath=@Image}" 
                       Width="16" 
                       Height="16" 
                       Margin="3" />
            </Border>
            <Border Background="{Binding XPath=@Background}" Grid.Column="1">
                <TextBlock Foreground="{Binding XPath=@Foreground}" 
                           Margin="3"
                           Text="{Binding XPath=@Property}" />
            </Border>
            <Border Background="{Binding XPath=@Background}" Grid.Column="2">
                <TextBlock Foreground="{Binding XPath=@Foreground}" 
                           Margin="3" 
                           FontWeight="Bold"
                           Text="{Binding XPath=@Value}" />
            </Border>
        </Grid>
    </DataTemplate>

    <XmlDataProvider x:Key="xmlNodeList" 
                     Source="/data/Combo.xml" 
                     XPath="/Root/Node"/>
</UserControl.Resources>

<ComboBox Name="myComboBox" 
          ItemsSource="{Binding Source={StaticResource xmlNodeList}}" 
          SelectedIndex="0" />

但看起来并不好: - (

您推荐什么解决方案?

提前致谢!

1 个答案:

答案 0 :(得分:1)

有一件事是您忘记指出要使用的模板:

<DataTemplate x:Key="Node">
      <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" MinWidth="20"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="Auto" MinWidth="20"/>
        </Grid.ColumnDefinitions>
        <Border Background="{Binding XPath=@Background}" Grid.Column="0">
            <Image Source="{Binding XPath=@Image}" 
                   Width="16" 
                   Height="16" 
                   Margin="3" />
        </Border>
        <Border Background="{Binding XPath=@Background}" Grid.Column="1">
            <TextBlock Foreground="{Binding XPath=@Foreground}" 
                       Margin="3"
                       Text="{Binding XPath=@Property}" />
        </Border>
        <Border Background="{Binding XPath=@Background}" Grid.Column="2">
            <TextBlock Foreground="{Binding XPath=@Foreground}" 
                       Margin="3" 
                       FontWeight="Bold"
                       Text="{Binding XPath=@Value}" />
        </Border>
    </Grid>
</DataTemplate>

然后你的ComboBox:

<ComboBox Name="myComboBox" 
          ItemsSource="{Binding Source={StaticResource xmlNodeList}}" 
          ItemTemplate="{StaticResource Node}"
          SelectedIndex="0" />

“dataType”系统适用于类型化对象,我不确定您是否可以使用XML数据。这将有效。

更新: 在您提出要求之前,您还应该为项目定义样式,以便它们覆盖列表的宽度,否则您的列将不均匀:

   <ComboBox Name="myComboBox"
                      ItemsSource="{Binding Source={StaticResource xmlNodeList}}"
                      ItemTemplate="{StaticResource Node}"
                      SelectedIndex="0">
                <ComboBox.ItemContainerStyle>
                    <Style TargetType="{x:Type ComboBoxItem}">
                        <Setter Property="HorizontalContentAlignment"
                                Value="Stretch" />
                    </Style>
                </ComboBox.ItemContainerStyle>
            </ComboBox>

这将拉伸单个项目以覆盖列表的整个宽度。