简体   繁体   中英

Databinding to combo box WPF XAML

I'm trying to bind data to a combo box. The data is names from a table from database. Getting the data from the database works fine as I have tried to bind it to a List View and it displays fine. My problem is binding it to a combo box, it doesn't show anything.

Can anyone see where I have went wrong?

My code is the following...

   public string FullName
    {
        get
        {
            return String.Format("{0} {1}", _customer.ContactFirstName, _customer.ContactLastName);
        }
    }

and the XAML is

    <ComboBox x:Name="comboBox" Grid.Row="1" Grid.ColumnSpan="2" Height="20" ItemsSource="{Binding Path=FullName}" >

The XAML for it working with a list view is the following..

<ListView 
     AlternationCount="2" 
     DataContext="{StaticResource WorkorderGroups}" 
     ItemContainerStyle="{StaticResource WorkorderItemStyle}"
     ItemsSource="{Binding}"
     Grid.Row="2" Grid.ColumnSpan="3" Margin="1,0,-1,0>

     <ListView.GroupStyle>
            <StaticResourceExtension ResourceKey="WorkorderGroupStyle/>
     </ListView.GroupStyle>

        <ListView.View>
            <GridView>
                <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Path=FullName}" /> 
            </GridView>
        </ListView.View>
    </ListView>

You cannot Bind a string to ComboBox. You need to bind some collection like List<T> or ObservableCollection<T> to combobox ItemsSource. Possible Duplicate. Refer the link. Need SIMPLE working example of setting WPF MVVM ComboBox ItemsSource based on SelectedValue of second ComboBox

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