Link to home
Start Free TrialLog in
Avatar of N P
N P

asked on

populating combobox

I want populate a combobox in WPF with the data that I have in LINQ to SQL result set.  I'm not sure what to do in the xaml side to hook the results that I got from LINQ to SQL.  I have 2 codes below

(1) my xaml for the dropdown combobox being used in WPF.
(2) my window_loaded routine getting the results into DataList

thanks
nick
<Window x:Class="WPF_Statement_View.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="651" Width="703" xmlns:igDP="http://infragistics.com/DataPresenter" Loaded="Window_Loaded">
    <Grid Height="516">
        <Grid.RowDefinitions>
            <RowDefinition Height="355*" />
            <RowDefinition Height="2*" />
        </Grid.RowDefinitions>
        <igDP:XamDataGrid HorizontalAlignment="Left" Margin="0,49,0,0" Name="xamDataGrid1" Width="681" Height="378" VerticalAlignment="Top" />
        <ComboBox Height="23" HorizontalAlignment="Left" Margin="64,12,0,0" Name="comboBox1" VerticalAlignment="Top" Width="171" ItemsSource="{Binding }" />
        <Label Content="Date" Height="28" HorizontalAlignment="Left" Margin="22,12,0,0" Name="label1" VerticalAlignment="Top" Width="65" />
    </Grid>
</Window>

Open in new window

namespace WPF_Statement_View
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

            Statements_ARDataContext db = new Statements_ARDataContext();

            var DateList = from t in db.StatementDates 
                                 select t;
    
        }
    }
}

Open in new window

Avatar of Vaughn Bigham
Vaughn Bigham
Flag of United States of America image

comboBox1.ItemSource = DateList

That should probably do the trick in code.  If you want to do it in XAML you be looking into a CollectionViewSource or something... I usually just set the itemssource in code.  But the ComboBox will want a DisplayMemberPath set in XAML (ex. <ComboBox Height="23" DisplayMemberPath="Date" Name="combobox1" />)
Avatar of N P
N P

ASKER

Thanks for replying vbigham.  I tried your solution.  The dropdown populates but it doesn't show the date.  The dropdown shows "WPF_statements_process.statementdate."  How can I get to the date.  Thanks. ..nick..
Ok, so the DisplayMemberPath will want to be the name of the field where you are actually storing the date.  So, I am not sure what WPF_statements_process.statementdate looks like, but I am thinking that you should do something like this DisplayMemberPath="statementdate.DESIREDFIELD"  replace DESIREDFIELD with the actual name of the date that you want to show up.
Actually, you might not even need the statementdate part, just the name of the field where the date is stored.
Avatar of N P

ASKER

Actual the route I went was using:

comboBox1.ItemSource = DateList

Do I still need to get involved with DisplayMemberPath in xml?  Currently it seems not to display the date that is captured in the DateList.  I'm not sure how to drilldown in the DateList to get to the dates.

thanks
nick
ASKER CERTIFIED SOLUTION
Avatar of Vaughn Bigham
Vaughn Bigham
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of N P

ASKER

great solution.  Thanks for the help