0

I implemented a column in a data grid that containes comboboxes. In order to display a text box in stead of a combobox when a list containes only one value, I used the solution from this post:

How to hide combobox toggle button if there is only one item?

However, when that one value in the list is changed, it is not updated in the text box. I have, of course, implemented INotifyPropertyChanged and it works as long as I have more than one item in the list (in other words, when the combobox is shown) but the value in the TextBlock is never updated.

Edit:

         <DataGridTemplateColumn.CellTemplate>
                                            <DataTemplate>
                                                <ComboBox Name="CList" ItemsSource="{Binding Values, UpdateSourceTrigger=PropertyChanged}" 
                                                          SelectedItem="{Binding Path=SelectedValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                                          SelectedIndex="0" BorderBrush="Transparent"
                                                          Background="Transparent">

                                                    <ComboBox.Style>
                                                        <Style TargetType="{x:Type ComboBox}" >
                                                            <Style.Triggers>
                                                                <DataTrigger Binding="{Binding Path=Items.Count, ElementName=CList}" Value="1">
                                                                    <Setter Property="Template">
                                                                        <Setter.Value>
                                                                            <ControlTemplate>
                                                                                <TextBlock Text="{Binding Items[0],  ElementName=CList}" />
                                                                            </ControlTemplate>
                                                                        </Setter.Value>
                                                                    </Setter>
                                                                </DataTrigger>
                                                            </Style.Triggers>
                                                        </Style>
                                                    </ComboBox.Style>
                                                </ComboBox>
                                            </DataTemplate>
                                        </DataGridTemplateColumn.CellTemplate>
Community
  • 1
  • 1
mobearette
  • 377
  • 10
  • 26

1 Answers1

1

I can see you are binding to the item itself, instead of any property in it

so perhaps you many need to bind to the respective property of your data item

eg

<TextBlock Text="{Binding Items[0].MyProperty, ElementName=CList}" />

assuming your intended property is MyProperty

Note if there is no underlying property then you'll have to remove the item and add new one again to list in order to update the text block, in this scenario INotifyPropertyChanged will also not work

pushpraj
  • 13,458
  • 3
  • 33
  • 50
  • I did not have any underlying properties so when updating the list to which I bind to I overwrote it twice, once with an empty list and then with the list with a new value in it. I guess the count of the items in the list has to change in order to update that value in the text box. Thank you very much. – mobearette Jun 25 '14 at 08:41
  • 1
    yes you are correct. seems like you are binding to a list of strings. I would suggest to bind with observable collection of string so you can simply use .ClearItems() or .RemoveItem(0) followed by .Add("new item") on the collection instead of replacing whole list twice – pushpraj Jun 25 '14 at 08:45