none
Refresh ComboBox with DataTemplate/Binding and IValueConverter RRS feed

  • Question

  • Hello all,

    i have a ComboBox with a DataTemplate, bindings and a IValueConverter, XAML Code is below:

     

    <ComboBox x:Name="MaterialComboBox" IsEditable="False" SelectedItem="{Binding Path=Material}" SelectionChanged="MaterialComboBox_SelectionChanged">
                  <ItemsControl.ItemTemplate>
                         <DataTemplate>
                                  <StackPanel Orientation="Horizontal">
                                            <Image Margin="0,0,5,0" Source="{Binding Converter={StaticResource producedPiecesConverter2}}" />
                                            <TextBlock Text="{Binding Path=code}"></TextBlock>
                                            <TextBlock Text=", "></TextBlock>
                                            <TextBlock Text="{Binding Path=description}"></TextBlock>
                                  </StackPanel>
                         </DataTemplate>
                  </ItemsControl.ItemTemplate>
    </ComboBox>

     

    I use the <image> inside the combobox to display a red circle or a green circle on each record, depending on the case, to show the user what records from combobox are correct and what are missing data, in the registry that are missing data the user can open it and edit the registry. I would like to after edit the registry the red circle change directly to a green circle.

    This code below is my Converter that fires in the <image> block from ComboBox

     

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                Material Chapa = (Material)value;       
                    var pieceGroups = from p in App.db.PieceGroups
                                      where p.isrepository == true &&
                                      p.Material.id == Chapa.id &&
                                      p.status == (byte)PieceGroupStatus.Created &&
                                      p.quantity > 0
                                      select p;
                    
                    if (pieceGroups.Count() < 1)
                    {
                        // RETURN RED ICON - NO Material
                        return "/OtimizeStandard;component/Resources/dark-red-circle.png";
                    }
                    else
                    {
                        return "/OtimizeStandard;component/Resources/dark-green-circle.png";
                    }
                    
           }

     But the ComboBox is not updating, it just update when i re-open the program or close the current screen and go to another screen of the program.

    Anybody know how I could reload or refresh the combobox?

     

    Regards,

    Marius Edelsbrunner

    Tuesday, April 13, 2010 1:44 PM

Answers

  • Currently you are binding to the Material object itself and then, in the converter, checking to see whether or not the object has any children. I recommend that you create a custom dependency property called "HasChildren" of type boolean and then bind your image to that, using the converter to select the appropriate image. You can then update the HasChildren property when appropriate and WPF will automatically catch the change and update all controls that are bound to that property, such as your Image.

    If your Material object is not a dependency object (in other words, you cannot create dependency properties for it), you could just use a regular property. Make sure to implement System.ComponentModel.INotifyPropertyChanged and then raise the PropertyChanged event when the HasChildren property is Set, otherwise WPF will have no way of knowing that the property has been updated.

    Tuesday, April 13, 2010 2:04 PM
  • Hi Igor and Marius,

    Based on your description, I think it is unnecessary to do something about notify the UI from the Entity Framework. I have a suggestion that is to specify the ItemsSource of the ComboBox to your PieceGroups object and specify the SelectedItem to the Material object. Of cause, both classes should implement the interface "INotifyCollectionChanged" and "INotifyPropertyChanged". Then any changes in the Entity can update to the ComboBox.

    Sincerely,

    Bob Bao


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Thursday, April 15, 2010 3:18 AM
    Moderator

All replies

  • Currently you are binding to the Material object itself and then, in the converter, checking to see whether or not the object has any children. I recommend that you create a custom dependency property called "HasChildren" of type boolean and then bind your image to that, using the converter to select the appropriate image. You can then update the HasChildren property when appropriate and WPF will automatically catch the change and update all controls that are bound to that property, such as your Image.

    If your Material object is not a dependency object (in other words, you cannot create dependency properties for it), you could just use a regular property. Make sure to implement System.ComponentModel.INotifyPropertyChanged and then raise the PropertyChanged event when the HasChildren property is Set, otherwise WPF will have no way of knowing that the property has been updated.

    Tuesday, April 13, 2010 2:04 PM
  • Hello Cyborgx372,

    I'm on the same project with Marius. We have developed the changes you suggested using the INotifyPropertyChanged interface and it worked as expected. Thank you very much for the help.

    However, we noticed that having to manually set the "HasChildren" property only to notify the WPF binding mechanism to fire the converter method is something not very automatic in terms of binding.

    Now we have to catch every event that generates a new Children on our Material class and set the HasChanged property manually.

    In our cenario, Material is an object form the Entity Framework, and we had to implement a partial Material class to add the HasChildren property (including the NotifyPropertyChanged interface).

    The Material has an 1 to N relation with PieceGroups objects, that we are actually checking to see whether we have or not children. Do you think I could use something on the Entity Framework to notify my UI about the changes?

    Kindly Regards,

    Igor Kondrasovas.

     


    Project Manager at INOVATIVA Tecnologia www.inovativatec.com
    Tuesday, April 13, 2010 7:54 PM
  • Hi Igor and Marius,

    Based on your description, I think it is unnecessary to do something about notify the UI from the Entity Framework. I have a suggestion that is to specify the ItemsSource of the ComboBox to your PieceGroups object and specify the SelectedItem to the Material object. Of cause, both classes should implement the interface "INotifyCollectionChanged" and "INotifyPropertyChanged". Then any changes in the Entity can update to the ComboBox.

    Sincerely,

    Bob Bao


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Thursday, April 15, 2010 3:18 AM
    Moderator