
I am trying to bind the SelectedItem of a ColorPickerGallery to the image of a SplitButton.
I have a Brush to ImageSource converter for the binding.
If I programmaticaly use the SelectedItemChanged event and call my converter it works. However, if I use a binding the converter call is never called, however the converter is instantiated.
Note my converter is using a base class that implements IValueConverter
Any ideas?
Thanks,
Ken
Here is the xaml:
<ribbon:SplitButton ImageSourceSmall="{Binding Source=BackgroundColorGallery, Path=SelectedItem, Converter={StaticResource BrushToImageConverter}, Mode=OneWay}" Label="Background" VariantSize="Medium">
<StackPanel>
<ribbon:ColorPickerGallery x:Name="BackgroundColorGallery" InitialColumnCount="10" HorizontalAlignment="Center" IsPreviewEnabled="True">
<ribbon:ColorPickerGallery.CategorizedItemsSource>
<x:Array Type="{x:Type media:SolidColorBrush}">
<media:SolidColorBrush ribbon:PopupGallery.Category="Theme Colors" ribbon:ColorPickerGallery.LayoutBehavior="Shaded" Color="#FFFFFF" ribbon:ScreenTipService.ScreenTipHeader="White" />
<media:SolidColorBrush ribbon:PopupGallery.Category="Theme Colors" ribbon:ColorPickerGallery.LayoutBehavior="Shaded" Color="#000000" ribbon:ScreenTipService.ScreenTipHeader="Black" />
<media:SolidColorBrush ribbon:PopupGallery.Category="Theme Colors" ribbon:ColorPickerGallery.LayoutBehavior="Shaded" Color="#EEECE1" ribbon:ScreenTipService.ScreenTipHeader="Tan" />
<media:SolidColorBrush ribbon:PopupGallery.Category="Theme Colors" ribbon:ColorPickerGallery.LayoutBehavior="Shaded" Color="#1F497D" ribbon:ScreenTipService.ScreenTipHeader="Dark Blue" />
<media:SolidColorBrush ribbon:PopupGallery.Category="Theme Colors" ribbon:ColorPickerGallery.LayoutBehavior="Shaded" Color="#4F81BD" ribbon:ScreenTipService.ScreenTipHeader="Blue" />
<media:SolidColorBrush ribbon:PopupGallery.Category="Theme Colors" ribbon:ColorPickerGallery.LayoutBehavior="Shaded" Color="#C0504D" ribbon:ScreenTipService.ScreenTipHeader="Red" />
<media:SolidColorBrush ribbon:PopupGallery.Category="Theme Colors" ribbon:ColorPickerGallery.LayoutBehavior="Shaded" Color="#9BBB59" ribbon:ScreenTipService.ScreenTipHeader="Olive Green" />
<media:SolidColorBrush ribbon:PopupGallery.Category="Theme Colors" ribbon:ColorPickerGallery.LayoutBehavior="Shaded" Color="#8064A2" ribbon:ScreenTipService.ScreenTipHeader="Purple" />
<media:SolidColorBrush ribbon:PopupGallery.Category="Theme Colors" ribbon:ColorPickerGallery.LayoutBehavior="Shaded" Color="#4BACC6" ribbon:ScreenTipService.ScreenTipHeader="Aqua" />
<media:SolidColorBrush ribbon:PopupGallery.Category="Theme Colors" ribbon:ColorPickerGallery.LayoutBehavior="Shaded" Color="#F79646" ribbon:ScreenTipService.ScreenTipHeader="Orange" />
<media:SolidColorBrush ribbon:PopupGallery.Category="Standard Colors" Color="#C00000" ribbon:ScreenTipService.ScreenTipHeader="Dark Red" />
<media:SolidColorBrush ribbon:PopupGallery.Category="Standard Colors" Color="#FF0000" ribbon:ScreenTipService.ScreenTipHeader="Red" />
<media:SolidColorBrush ribbon:PopupGallery.Category="Standard Colors" Color="#FFC000" ribbon:ScreenTipService.ScreenTipHeader="Orange" />
<media:SolidColorBrush ribbon:PopupGallery.Category="Standard Colors" Color="#FFFF00" ribbon:ScreenTipService.ScreenTipHeader="Yellow" />
<media:SolidColorBrush ribbon:PopupGallery.Category="Standard Colors" Color="#92D050" ribbon:ScreenTipService.ScreenTipHeader="Light Green" />
<media:SolidColorBrush ribbon:PopupGallery.Category="Standard Colors" Color="#00B050" ribbon:ScreenTipService.ScreenTipHeader="Green" />
<media:SolidColorBrush ribbon:PopupGallery.Category="Standard Colors" Color="#00B0F0" ribbon:ScreenTipService.ScreenTipHeader="Light Blue" />
<media:SolidColorBrush ribbon:PopupGallery.Category="Standard Colors" Color="#0070C0" ribbon:ScreenTipService.ScreenTipHeader="Blue" />
<media:SolidColorBrush ribbon:PopupGallery.Category="Standard Colors" Color="#002060" ribbon:ScreenTipService.ScreenTipHeader="Dark Blue" />
<media:SolidColorBrush ribbon:PopupGallery.Category="Standard Colors" Color="#7030A0" ribbon:ScreenTipService.ScreenTipHeader="Purple" />
</x:Array>
</ribbon:ColorPickerGallery.CategorizedItemsSource>
</ribbon:ColorPickerGallery>
<ribbon:Separator />
<ribbon:Menu>
<ribbon:Button Label="More Colors..." />
</ribbon:Menu>
</StackPanel>
</ribbon:SplitButton>
Here is the converter:
namespace PARCGraphicsWPF
{
[Serializable]
public class PVBrushToImageConverter : PVConverter
{
#region IValueConverter Members
public PVBrushToImageConverter()
{
}
public PVBrushToImageConverter(ImageSource DefaultImage)
{
}
public override object Convert(object value, System.Type targetType, object parameter, CultureInfo culture)
{
if ((value is SolidColorBrush) && (value != null))
{
try
{
System.Windows.Shapes.Rectangle rect = new Rectangle();
rect.Height = 16;
rect.Width = 16;
rect.Fill = value as SolidColorBrush;
return GetImageOfControl(rect);
}
catch (Exception)
{
return DependencyProperty.UnsetValue;
}
}
else
{
return DependencyProperty.UnsetValue;
}
return DependencyProperty.UnsetValue;
}
public override object ConvertBack(object value, System.Type targetType, object parameter, CultureInfo culture)
{
return DependencyProperty.UnsetValue;
}
#endregion
private static PngBitmapEncoder getImageFromControl(Shape controlToConvert)
{
// save current canvas transform
Transform transform = controlToConvert.LayoutTransform;
// get size of control
Size sizeOfControl = new Size(controlToConvert.Width, controlToConvert.Height);
// measure and arrange the control
controlToConvert.Measure(sizeOfControl);
// arrange the surface
controlToConvert.Arrange(new Rect(sizeOfControl));
// craete and render surface and push bitmap to it
RenderTargetBitmap renderBitmap = new RenderTargetBitmap((Int32)sizeOfControl.Width, (Int32)sizeOfControl.Height, 96d, 96d, PixelFormats.Pbgra32);
// now render surface to bitmap
renderBitmap.Render(controlToConvert);
// encode png data
PngBitmapEncoder pngEncoder = new PngBitmapEncoder();
// puch rendered bitmap into it
pngEncoder.Frames.Add(BitmapFrame.Create(renderBitmap));
// return encoder
return pngEncoder;
}
/// <summary>
/// Get an ImageSource of a control
/// </summary>
/// <param name="controlToConvert">The control to convert to an ImageSource</param>
/// <returns>The returned ImageSource of the controlToConvert</returns>
public static ImageSource GetImageOfControl(Shape controlToConvert)
{
// return first frame of image
return getImageFromControl(controlToConvert).Frames[0];
}
}
}
I have a Brush to ImageSource converter for the binding.
If I programmaticaly use the SelectedItemChanged event and call my converter it works. However, if I use a binding the converter call is never called, however the converter is instantiated.
Note my converter is using a base class that implements IValueConverter
Any ideas?
Thanks,
Ken
Here is the xaml:
<ribbon:SplitButton ImageSourceSmall="{Binding Source=BackgroundColorGallery, Path=SelectedItem, Converter={StaticResource BrushToImageConverter}, Mode=OneWay}" Label="Background" VariantSize="Medium">
<StackPanel>
<ribbon:ColorPickerGallery x:Name="BackgroundColorGallery" InitialColumnCount="10" HorizontalAlignment="Center" IsPreviewEnabled="True">
<ribbon:ColorPickerGallery.CategorizedItemsSource>
<x:Array Type="{x:Type media:SolidColorBrush}">
<media:SolidColorBrush ribbon:PopupGallery.Category="Theme Colors" ribbon:ColorPickerGallery.LayoutBehavior="Shaded" Color="#FFFFFF" ribbon:ScreenTipService.ScreenTipHeader="White" />
<media:SolidColorBrush ribbon:PopupGallery.Category="Theme Colors" ribbon:ColorPickerGallery.LayoutBehavior="Shaded" Color="#000000" ribbon:ScreenTipService.ScreenTipHeader="Black" />
<media:SolidColorBrush ribbon:PopupGallery.Category="Theme Colors" ribbon:ColorPickerGallery.LayoutBehavior="Shaded" Color="#EEECE1" ribbon:ScreenTipService.ScreenTipHeader="Tan" />
<media:SolidColorBrush ribbon:PopupGallery.Category="Theme Colors" ribbon:ColorPickerGallery.LayoutBehavior="Shaded" Color="#1F497D" ribbon:ScreenTipService.ScreenTipHeader="Dark Blue" />
<media:SolidColorBrush ribbon:PopupGallery.Category="Theme Colors" ribbon:ColorPickerGallery.LayoutBehavior="Shaded" Color="#4F81BD" ribbon:ScreenTipService.ScreenTipHeader="Blue" />
<media:SolidColorBrush ribbon:PopupGallery.Category="Theme Colors" ribbon:ColorPickerGallery.LayoutBehavior="Shaded" Color="#C0504D" ribbon:ScreenTipService.ScreenTipHeader="Red" />
<media:SolidColorBrush ribbon:PopupGallery.Category="Theme Colors" ribbon:ColorPickerGallery.LayoutBehavior="Shaded" Color="#9BBB59" ribbon:ScreenTipService.ScreenTipHeader="Olive Green" />
<media:SolidColorBrush ribbon:PopupGallery.Category="Theme Colors" ribbon:ColorPickerGallery.LayoutBehavior="Shaded" Color="#8064A2" ribbon:ScreenTipService.ScreenTipHeader="Purple" />
<media:SolidColorBrush ribbon:PopupGallery.Category="Theme Colors" ribbon:ColorPickerGallery.LayoutBehavior="Shaded" Color="#4BACC6" ribbon:ScreenTipService.ScreenTipHeader="Aqua" />
<media:SolidColorBrush ribbon:PopupGallery.Category="Theme Colors" ribbon:ColorPickerGallery.LayoutBehavior="Shaded" Color="#F79646" ribbon:ScreenTipService.ScreenTipHeader="Orange" />
<media:SolidColorBrush ribbon:PopupGallery.Category="Standard Colors" Color="#C00000" ribbon:ScreenTipService.ScreenTipHeader="Dark Red" />
<media:SolidColorBrush ribbon:PopupGallery.Category="Standard Colors" Color="#FF0000" ribbon:ScreenTipService.ScreenTipHeader="Red" />
<media:SolidColorBrush ribbon:PopupGallery.Category="Standard Colors" Color="#FFC000" ribbon:ScreenTipService.ScreenTipHeader="Orange" />
<media:SolidColorBrush ribbon:PopupGallery.Category="Standard Colors" Color="#FFFF00" ribbon:ScreenTipService.ScreenTipHeader="Yellow" />
<media:SolidColorBrush ribbon:PopupGallery.Category="Standard Colors" Color="#92D050" ribbon:ScreenTipService.ScreenTipHeader="Light Green" />
<media:SolidColorBrush ribbon:PopupGallery.Category="Standard Colors" Color="#00B050" ribbon:ScreenTipService.ScreenTipHeader="Green" />
<media:SolidColorBrush ribbon:PopupGallery.Category="Standard Colors" Color="#00B0F0" ribbon:ScreenTipService.ScreenTipHeader="Light Blue" />
<media:SolidColorBrush ribbon:PopupGallery.Category="Standard Colors" Color="#0070C0" ribbon:ScreenTipService.ScreenTipHeader="Blue" />
<media:SolidColorBrush ribbon:PopupGallery.Category="Standard Colors" Color="#002060" ribbon:ScreenTipService.ScreenTipHeader="Dark Blue" />
<media:SolidColorBrush ribbon:PopupGallery.Category="Standard Colors" Color="#7030A0" ribbon:ScreenTipService.ScreenTipHeader="Purple" />
</x:Array>
</ribbon:ColorPickerGallery.CategorizedItemsSource>
</ribbon:ColorPickerGallery>
<ribbon:Separator />
<ribbon:Menu>
<ribbon:Button Label="More Colors..." />
</ribbon:Menu>
</StackPanel>
</ribbon:SplitButton>
Here is the converter:
namespace PARCGraphicsWPF
{
[Serializable]
public class PVBrushToImageConverter : PVConverter
{
#region IValueConverter Members
public PVBrushToImageConverter()
{
}
public PVBrushToImageConverter(ImageSource DefaultImage)
{
}
public override object Convert(object value, System.Type targetType, object parameter, CultureInfo culture)
{
if ((value is SolidColorBrush) && (value != null))
{
try
{
System.Windows.Shapes.Rectangle rect = new Rectangle();
rect.Height = 16;
rect.Width = 16;
rect.Fill = value as SolidColorBrush;
return GetImageOfControl(rect);
}
catch (Exception)
{
return DependencyProperty.UnsetValue;
}
}
else
{
return DependencyProperty.UnsetValue;
}
return DependencyProperty.UnsetValue;
}
public override object ConvertBack(object value, System.Type targetType, object parameter, CultureInfo culture)
{
return DependencyProperty.UnsetValue;
}
#endregion
private static PngBitmapEncoder getImageFromControl(Shape controlToConvert)
{
// save current canvas transform
Transform transform = controlToConvert.LayoutTransform;
// get size of control
Size sizeOfControl = new Size(controlToConvert.Width, controlToConvert.Height);
// measure and arrange the control
controlToConvert.Measure(sizeOfControl);
// arrange the surface
controlToConvert.Arrange(new Rect(sizeOfControl));
// craete and render surface and push bitmap to it
RenderTargetBitmap renderBitmap = new RenderTargetBitmap((Int32)sizeOfControl.Width, (Int32)sizeOfControl.Height, 96d, 96d, PixelFormats.Pbgra32);
// now render surface to bitmap
renderBitmap.Render(controlToConvert);
// encode png data
PngBitmapEncoder pngEncoder = new PngBitmapEncoder();
// puch rendered bitmap into it
pngEncoder.Frames.Add(BitmapFrame.Create(renderBitmap));
// return encoder
return pngEncoder;
}
/// <summary>
/// Get an ImageSource of a control
/// </summary>
/// <param name="controlToConvert">The control to convert to an ImageSource</param>
/// <returns>The returned ImageSource of the controlToConvert</returns>
public static ImageSource GetImageOfControl(Shape controlToConvert)
{
// return first frame of image
return getImageFromControl(controlToConvert).Frames[0];
}
}
}