简体   繁体   中英

WPF Combobox displaying default value from database

I'm using MongoDB database and I want to display data from MongoDB in combobox as default value. But i don't know how to do that.

This is my XAML

<DataGridTextColumn Header="Kaina" Binding="{Binding full_price}" CanUserReorder="False"
                                    IsReadOnly="True"/>
<DataGridTextColumn Header="DV" Binding="{Binding order_quantity}" CanUserReorder="False"
                    IsReadOnly="True"/>
<DataGridTemplateColumn Header="Stalviršio tipas">
  <DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
      <ComboBox x:Name="TableTop" DropDownClosed="TableTop_DropDownClosed" Text="fg">
        <ComboBoxItem Content="A" />
        <ComboBoxItem Content="B" />
        <ComboBoxItem Content="C" />
      </ComboBox>
    </DataTemplate>
  </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

This is my MongoDB database documents:

_id:"B0/0045/0214-05_FP0-E202-130/70/90"
electricity_based_on:"FP0-P202"
full_price:1225.95
manufacturing_date:2020-02-13T22:00:00.000+00:00
order_code:"FP0-E202-130/70/90"
order_comment_names:"1) Stalviršis su standartiniu borteliu FP0-000-12.00 SB,
order_name:"Šaldomas stalas 2 durelių"
order_number:"B0/0045/0214-05"
order_quantity:1
unit_price:1225.95
tabletop_letter:"C"
sub3_4:""

I want to display "tabletop_letter" from MongoDB to "TableTop" Combobox as default value. Can you help me?

I found this link How to display default text "--Select Team --" in combo box on pageload in WPF? applied some modifications and it works.

<DataGridTemplateColumn Header="Stalviršio tipas">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Grid>
                                <ComboBox x:Name="TableTop" DropDownClosed="TableTop_DropDownClosed" ItemsSource="{Binding}">
                                    <ComboBoxItem Content="" />
                                    <ComboBoxItem Content="A" />
                                    <ComboBoxItem Content="B" />
                                    <ComboBoxItem Content="C" />
                                </ComboBox>
                                <TextBlock Text="{Binding tabletop_letter}" IsHitTestVisible="False">
                                    <TextBlock.Style>
                                        <Style TargetType="TextBlock">
                                            <Setter Property="Visibility" Value="Hidden"/>
                                            <Style.Triggers>
                                                <DataTrigger Binding="{Binding ElementName=TableTop,Path=SelectedItem}" Value="{x:Null}">
                                                    <Setter Property="Visibility" Value="Visible"/>
                                                </DataTrigger>
                                            </Style.Triggers>
                                        </Style>
                                    </TextBlock.Style>
                                </TextBlock>
                            </Grid>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM