SplitButton Icon customization problem

Nov 7, 2012 at 2:16 PM
Edited Nov 7, 2012 at 2:17 PM

Hi !

I've customized ToggleButton Icon as in the project example, and it works great.

 <Fluent:ToggleButton Header="blah blah" SizeDefinition="Small" >
    <Fluent:ToggleButton.Icon>
       <Grid>
            <Image Source="animage " />
            <TextBlock Text="1" />
        </Grid>
    </Fluent:ToggleButton.Icon>
 </Fluent:ToggleButton>

I tried to do the same thing with a SplitButton, but there is an exception in ToggleButton.cs :

Exception :

System.InvalidOperationException was unhandled by user code
  Message=Specified element is already the logical child of another element. Disconnect it first.
  Source=PresentationFramework
  StackTrace:
       at System.Windows.FrameworkElement.ChangeLogicalParent(DependencyObject newParent)
       at System.Windows.FrameworkElement.AddLogicalChild(Object child)
       at Fluent.ToggleButton.OnIconChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) in C:\PRIV\Projects\ESD1\ESD1_MOCKUPS\Fluent\ToggleButton.cs:line 142
       at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
       at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
       at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
       at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
       at System.Windows.FrameworkTemplate.ReceivePropertySet(Object targetObject, XamlMember member, Object value, DependencyObject templatedParent)
       at System.Windows.FrameworkTemplate.<>c__DisplayClass6.<LoadOptimizedTemplateContent>b__4(Object sender, XamlSetValueEventArgs setArgs)
       at System.Xaml.XamlObjectWriter.OnSetValue(Object eventSender, XamlMember member, Object value)
       at System.Xaml.XamlObjectWriter.SetValue(Object inst, XamlMember property, Object value)
       at System.Xaml.XamlObjectWriter.Logic_ApplyPropertyValue(ObjectWriterContext ctx, XamlMember prop, Object value, Boolean onParent)
       at System.Xaml.XamlObjectWriter.Logic_DoAssignmentToParentProperty(ObjectWriterContext ctx)
       at System.Xaml.XamlObjectWriter.Logic_AssignProvidedValue(ObjectWriterContext ctx)
       at System.Xaml.XamlObjectWriter.WriteEndObject()
       at System.Xaml.XamlWriter.WriteNode(XamlReader reader)
       at System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlReader templateReader, XamlObjectWriter currentWriter)
  InnerException:

 

Problematic code :

      private static void OnIconChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ToggleButton element = d as ToggleButton;
            FrameworkElement oldElement = e.OldValue as FrameworkElement;
            if (oldElement != null) element.RemoveLogicalChild(oldElement);
            FrameworkElement newElement = e.NewValue as FrameworkElement;
            if (newElement != null) element.AddLogicalChild(newElement);
        }



In that case, the ToggleButton is the one in SplitButton template, and newElement (Grid containing Image + TextBlock) already in SplitButton logical tree :/
Any idea of a work around ?

Developer
Nov 8, 2012 at 8:57 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Developer
Nov 8, 2012 at 8:59 PM

Fixed with http://fluent.codeplex.com/SourceControl/changeset/c8ff24dac343