A
A
Anton2017-04-22 21:31:24
WPF
Anton, 2017-04-22 21:31:24

How to display in combobox a field composed of parts?

My regards!
Help to solve the problem of displaying combobox.
I have a table in the database, described by a class.
The class has two properties - FirstName and LastName.
I need to display both items in combobox at once.
Before that, I read the Internet and showed the property of the partial class, which can help solve the problem. At first glance, yes. I made a new property, which I called FullName, I declared that it is formed as a concatenation of the LastName and FullName fields. And I realized that I lacked knowledge or some understanding of mechanics. In principle, this is the same thing (:
See how I wrote above - I use the Code First approach. Accordingly, when I declared a new field, it was added to the table. What the studio told me about, demanding to update the database.
Where did I go wrong and how can I solve the problem?
Thank you.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Anton, 2017-04-30
@aszhitarev

everything turned out to be much simpler
[NotMapped]
public string ManagerFullName { get { return ManagerLastName + " "+ ManagerFirstName; } }

R
Roman, 2017-04-29
@yarosroman

If in the model you need some kind of auto-calculated field or a property of which should not be in the database, then it must be marked with the [NotMapped] attribute.

W
WarFollowsMe, 2017-04-23
@WarFollowsMe

Describe your ItemTemplate for the ComboBox.

<ComboBox ItemsSource={Binding Items}>
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding FirstName}"/>
                <TextBlock Text="{Binding LastName}"/>
            </StackPanel>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

The main thing you should understand is that the model obtained by the "Code-First approach" alone is not enough for WPF. You need to create a ViewModel in which you need to implement INotifyPropertyChanged. And already throw this viewmodel into the DataContext on the view.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question