WPF ComboBox来自数据库的多个列

时间:2014-12-22 22:23:01

标签: wpf database combobox datatemplate

我想在ComboBox中显示三列。

这是我的解决方案,但不幸的是它不起作用。

我有一个在程序启动时运行的方法,如下所示:

SqlDataAdapter da = new SqlDataAdapter("Select neve, kialakitas,befogado_kepesseg from helyszin", c1);
        DataSet ds = new DataSet();
        da.Fill(ds, "hely");
        cbHelyszin.ItemsSource = ds.Tables["hely"].DefaultView;
        cbHelyszin.DisplayMemberPath = ds.Tables["hely"].Columns["neve"].ToString();
        cbHelyszin.DisplayMemberPath = ds.Tables["hely"].Columns["kialakitas"].ToString();
        cbHelyszin.DisplayMemberPath = ds.Tables["hely"].Columns["befogado_kepesseg"].ToString();
        cbHelyszin.SelectedValuePath = ds.Tables["hely"].Columns["neve"].ToString();

XAML

<ComboBox.ItemContainerStyle>
                <Style TargetType="{x:Type ComboBoxItem}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate>
                                <Grid x:Name="gd" TextElement.Foreground="Black">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition/>
                                        <ColumnDefinition/>
                                        <ColumnDefinition/>
                                    </Grid.ColumnDefinitions>
                                    <TextBlock Margin="5" Grid.Column="0" Text="{Binding }"/>
                                    <TextBlock Margin="5" Grid.Column="1" Text="{Binding }"/>
                                    <TextBlock Margin="5" Grid.Column="2" Text="{Binding }"/>
                                </Grid>
                                <ControlTemplate.Triggers>
                                    <Trigger Property="ComboBoxItem.IsSelected" Value="True">
                                        <Setter TargetName="gd"  Property="Background" Value="Gray"></Setter>
                                        <Setter TargetName="gd"  Property="TextElement.Foreground" Value="White"></Setter>
                                    </Trigger>
                                    <Trigger Property="ComboBoxItem.IsMouseOver" Value="True">
                                        <Setter TargetName="gd"  Property="Background" Value="Blue"></Setter>
                                        <Setter TargetName="gd"  Property="TextElement.Foreground" Value="White"></Setter>
                                    </Trigger>                                       
                                </ControlTemplate.Triggers>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </ComboBox.ItemContainerStyle>
        </ComboBox>

如何使这些信息适合我的ComboBox?

1 个答案:

答案 0 :(得分:0)

您可以使用ItemTemplate而不是尝试将DisplayMemberPath设置三次。例如:

<ComboBox.ItemTemplate>
    <DataTemplate>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <TextBlock Text="{Binding neve}" />
            <TextBlock Grid.Column="1" Text="{Binding kialakitas}" />
            <TextBlock Grid.Column="2" Text="{Binding befogado_kepesseg}" />
        </Grid>
    </DataTemplate>
</ComboBox.ItemTemplate>