Disapearing Combobox Content

Sep 9, 2008 at 4:18 PM
I've got kind of a weird use case here that I'm trying to get right.  basically, I'm allowing the user to choose a reference to a previously defined parameter in an XML.  Here's my relevant XAML:

                   <tool:DataGridTemplateColumn Header="Type" >
                                    <StackPanel Orientation="Horizontal">
                                        <TextBlock Text="{Binding XPath=@ref, Mode=OneWay}" />
                                    <StackPanel Orientation="Horizontal">
                                        <ComboBox ItemsSource="{Binding XPath=//a:Object/@name}" SelectedValue="{Binding XPath=@ref}" />

The problem is, when I switch cells, the ComboBox clears itself.  I've actually put in a column just to show me the @ref property, and it is updating properly.  Even CommittingEdit shows that the proper parameter is being bound, so something else in the Data binding system is changing it, but I can't figure out what.  Note that this works with a regular ComboBoxColumn (if I bind the combo box during "PreparingCellForEdit") but I'm adding other controls to the templates eventually, so I'd like it to work this way.  Any ideas?
Sep 9, 2008 at 5:07 PM
I've seen something like this before.  What about setting SelectedValuePath on the ComboBox.  Does that help at all?
Sep 9, 2008 at 6:09 PM
Not sure what I'd set it to.  I set it to @ref and @name and neither seems to work properly.
Sep 9, 2008 at 7:03 PM
could you post the xml that you are using or at least a snippet of a couple sets.
Sep 9, 2008 at 7:48 PM
Here's an example:

<Object name="Main">
     <Event name="ObjectReferenced">
        <Parameter name="DataPoint1" type="String" />
        <Parameter name="DataPoint2" type="Int" />
        <Parameter name="ObjectReference" type="ObjectReference" ref="ReferencedObject" />
<Object name="ReferencedObject" />
<Object name="AnotherObject" />

Note, I put an event in to grab whenever the combo box changed selections.  When the cell goes away from editing, for some reason the combo box is completely unbound (Items.Count is 0).  That could be the problem, though I'm not sure why it would do that...
Sep 9, 2008 at 9:46 PM
The Items.Count being zero is caused by the fact that the DataContext is changing to null when the edit portion of the cell is removed.  It would seem to me that anything that relies on the data context to do its binding would have this problem.
Sep 9, 2008 at 10:05 PM
Although not actually solving the problem, I've been able to get around the problem with the following code:
       private void dgParameters_CommittingEdit(object sender, DataGridEndingEditEventArgs e)
            if(e.Column != null && e.Column.Header.ToString() == "Type")
                ComboBox cbReference = (ComboBox)e.EditingElement.FindName("cbReference");
                BindingOperations.ClearBinding(cbReference, ComboBox.SelectedValueProperty);

Though I'm curious also, why does CommittingEdit occasionally give you a null column?
Sep 10, 2008 at 4:29 PM
When you are committing a row, the column is purposely null since committing a row is not commiting a single cell.