Click here to Skip to main content
15,885,978 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello, i'm studying the mvvm pattern with c#. I've a problem!
I have a datagrid with an ItemSource (class "Order") and a SelectedItem ("SelectedOrder").
With a select i fill a form with the information conteined in the datagrid, but for some values i need to proposing some choice with combobox.
The choices of the combobox are not in the selecteditem.
How can I display the detail of my selected article of data and give the possibility to the user to choice from a list of value (OPEN, CLOSE, ...).
Thanks in advance

What I have tried:

This is the view
<pre>
<Grid DataContext="{Binding SelectedOrder}">
...
<Label Content="Customer:" FontSize="12" x:Name="lblState" VerticalAlignment="Top" Grid.Row="2" Grid.Column="2"/>
<ComboBox x:Name="cmbState" FontSize="12" FontWeight="Normal" IsEditable="True"  Grid.Row="2" Grid.Column="3" Text="{Binding state,Mode=TwoWay}">
...
</Grid>

<DataGrid ItemsSource="{Binding Order, IsAsync=True}" SelectedItem="{Binding SelectedOrder, Mode=TwoWay}" Grid.Row="3" AutoGenerateColumns="false" CanUserAddRows="False" CanUserSortColumns="True" CanUserResizeRows="False" IsReadOnly="True" SelectionMode="Single" SelectionUnit="FullRow">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Number" Binding="{Binding numOrder}" Width="1*" />


This is the class Order

public class Order
    {
        public int id { get; set; }
        public string numOrder { get; set; }
        public string title { get; set; }
        public string state { get; set; }
        public string dateCreation { get; set; }
        public string note { get; set; }
    }
Posted
Updated 21-Oct-18 13:38pm

1 solution

iI did a quick Google Search: wpf datagrid combobox column binding mvvm - Google Search[^]

The first result is a working answer for you: Binding a WPF DataGridComboBoxColumn with MVVM - Stack Overflow[^]
 
Share this answer
 
Comments
Member 11091382 22-Oct-18 7:46am    
Thanks for the reply, but actually is not the right answer for mine problem.
I'll try to better explain it:
I have a DataGrid with a main DataContext:

<Grid DataContext="{Binding CommessaDataContext}">
    ...
    <DataGrid ItemsSource="{Binding Orders, IsAsync=True}" SelectedItem="{Binding SelectedOrder, Mode=TwoWay}">
    ...
    </DataGrid>
</Grid>


When I select one order from datagrid i would to show up a detail of it in a nested grid with some TextBox and some ComboBox:

<Grid DataContext="{Binding SelectedCommessa}">
    ...
    <TextBox x:Name="selectedOrderNum" Grid.Row="1" Grid.Column="1" FontSize="14" Text="{Binding numOrder,Mode=TwoWay}" />
    ...
    <ComboBox x:Name="SelectedOrderState" Grid.Row="2"  Grid.Column="1" FontSize="12" IsEditable="True" Text="{Binding state,Mode=TwoWay}"  ItemsSource="{Binding orderStatus}"/>
</Grid>


The data-link from the selection and the filling of textboxes works fine, but i can't to preventively fill the ComboBox with other values, and show only object's value.

It's possible to nest a DataContext or ItemsSource inside a other DataContext?
Graeme_Grant 22-Oct-18 11:37am    
The answer given still applies.

The ComboBox.ItemsSource uses OrderStatus which should be a collection of valid States. The binding needs to be a relative reference to a DataContext where the collection can be found.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900