0
<DataGridTemplateColumn Header="Brand Code">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <ComboBox IsEditable="True" Text="{Binding Path=BrandCode, Mode=OneWayToSource}" ItemsSource="{Binding Brands}"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>

the above code displays the list but when i select the value it doesn't assign back to BrandCode.

but if i click on the next column to edit the value is assigned to BrandCode,

can any one help?

LeBlues
  • 305
  • 2
  • 5
  • 20

1 Answers1

0

Try this instead:

<ComboBox IsEditable="True" SelectedItem="{Binding Path=BrandCode, 
    Mode=OneWayToSource}" ItemsSource="{Binding Brands}"/>

It depends what is in your items, but if your items are instances of a class and you want to return a particular property value, you could also try this:

<ComboBox IsEditable="True" SelectedValue="{Binding Path=BrandCode, 
    Mode=OneWayToSource}" SelectedValuePath="NameOfPropertyToReturn" 
    ItemsSource="{Binding Brands}"/>

I've just tested that definitely works... if it doesn't work for you, you have a problem lying elsewhere in your code:

<StackPanel>
    <ComboBox ItemsSource="{Binding Brands}" SelectedItem="{Binding BrandCode}" />
    <TextBlock Text="{Binding SelectedValue}" />
</StackPanel>

UPDATE >>>

After seeing your latest code for your data class, I can see your problem... how have you been able to write any WPF without learning about the INotifyPropertyChanged interface? The two go hand in hand. Your solution is to implement this interface in your Data class. Please see the INotifyPropertyChanged Interface page at MDSN for help with this.

Please note that if you had shown us all your code in the first place, we could have fixed this problem immediately.

Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • hi i have List which is "Brands" and a string BrandCode where i need the return value to be in. each item in the datagrid column comes from a list of object which contain Brands and BrandCode. the above recommendation doesn't work – LeBlues Sep 19 '13 at 14:16
  • That's strange... the first example *should* work... what do the error(s) in the Output Window say? – Sheridan Sep 19 '13 at 14:31
  • i know right?, no error when i try to access BrandCode, it returns null :-( – LeBlues Sep 19 '13 at 14:35
  • let me give you my `code`class public class Data { public List Brands { get; set; } // contains brand names public string BrandCode { get; set; } / should allow a user to select or enter one if it is not in the list } `code` still doesn't work – LeBlues Sep 19 '13 at 15:14
  • also users should be able to change the existing brand – LeBlues Sep 19 '13 at 15:16
  • Dude!! Don't put code into comments... it *never* comes out right... add it onto the end of your question. – Sheridan Sep 19 '13 at 15:26
  • i do have INotifyPropertyChanged implemented (i didn't include that in the code above) – LeBlues Sep 19 '13 at 15:58
  • From the information that you've provided, I can't help you any further. You have a problem somewhere, but you haven't provided any code that contains the error, so there's nothing more that I can do for you at this point. – Sheridan Sep 19 '13 at 16:04