Search code examples
c#wpfcomboboxselectedvalue

Selected ComboBox value from EntityModel WPF when form loaded


When I run my project , Combobox don't select any item. But if I create Messagebox before loading it into Combobox, MessageBox displays value. Please help me:

my XAML code :

<Window x:Class="WpfApp1.MainWindow"
         xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        TextElement.Foreground="{DynamicResource MaterialDesignBody}"
     TextElement.FontWeight="Regular"
     TextElement.FontSize="13"
     TextOptions.TextFormattingMode="Ideal"
     TextOptions.TextRenderingMode="Auto"
     Background="{DynamicResource MaterialDesignPaper}"
     FontFamily="{DynamicResource MaterialDesignFont}"
    Title="MainWindow" Height="450" Width="800" Loaded="Window_Loaded">
    <Grid>
        <ComboBox x:Name="ComboboxTest" Background="White" HorizontalAlignment="Left" Margin="78,195,0,0" VerticalAlignment="Top" Width="139" IsEditable="True"  IsTextSearchEnabled="True"  IsEnabled="True" FlowDirection="RightToLeft"  FontFamily="IRANSans" FontSize="14" TabIndex="1" Focusable="True"  IsSynchronizedWithCurrentItem="True" 
                             ItemsSource="{Binding}" 
                             DisplayMemberPath="TitleNoeArz"
                             SelectedValuePath="IDNoeArz"
                             SelectedValue="{Binding IDNoeArz}" Height="32">

        </ComboBox>  

    </Grid>

</Window>

and my CS code

 private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                SBGharardadEntities sBGharardadEntities = new SBGharardadEntities();
                ComboboxTest.ItemsSource = sBGharardadEntities.NoeArzs.ToList();
                ComboboxTest.SelectedValue= 2;

            }
            catch (Exception er)
            {
                MessageBox.Show(er.Message);
            }
        }

and if u find a error than highlighting by means of code will be much better.

Thank you


Solution

  • *please try this
    
    
      <telerik:RadComboBox Grid.Row="5" Grid.Column="1" 
                                     x:Name="SiteAddressWatermark"
                                     IsEditable="True" DisplayMemberPath="SiteAddress" 
                                    SelectedValuePath="Id" IsReadOnly="False"/>
    
    
    
      using (var db = new ShareGateEntities())
                {
                    var collections = db.Connections.ToList();
                    SiteAddressWatermark.ItemsSource = collections;
    }*