Image ComboBox in WPF

Image ComboBox in WPF

reylorente1
Collaborator Collaborator
1,196 Views
3 Replies
Message 1 of 4

Image ComboBox in WPF

reylorente1
Collaborator
Collaborator

How to put an image in Combox through a Family Symbol with a Behind C # code WPF?

0 Likes
1,197 Views
3 Replies
Replies (3)
Message 2 of 4

Sean_Page
Collaborator
Collaborator

Images in Combobox:

https://www.c-sharpcorner.com/article/add-text-with-image-button-in-combobox-in-wpf-application/

 

Get Type images preview:

https://thebuildingcoder.typepad.com/blog/2010/05/get-type-id-and-preview-image.html

Sean Page, AIA, NCARB, LEED AP
Partner, Computational Designer, Architect
0 Likes
Message 3 of 4

reylorente1
Collaborator
Collaborator

I already have the event, but when I want to put the image in the Combobox, the GetPreviewImage gets a Bitmap, as I take it to image in WPF

 

<Window x:Class="WpfRevit.Col_Ejemplares"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfRevit"
mc:Ignorable="d" Title="WPF Colocar Ejemplar " WindowStartupLocation="CenterScreen" Width="400" Height="550" ResizeMode="CanResizeWithGrip">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="230"/>
<RowDefinition Height="40"/>
</Grid.RowDefinitions>
<GroupBox >
<GroupBox.Header>
<TextBlock Text="Coordenadas" FontWeight="Bold" FontSize="20"/>
</GroupBox.Header>
<DataGrid />
</GroupBox>
<GroupBox Grid.Row="1">
<GroupBox.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Opciones" FontWeight="Bold" FontSize="20"/>
</StackPanel>
</GroupBox.Header>
</GroupBox>
<ComboBoxItem Grid.Row="1">
<Border Background="Lavender" BorderBrush="Gainsboro" BorderThickness="2" Grid.Row="1" Height="220">
<WrapPanel >
<Label Content="Categorias:" Width="148" VerticalAlignment="Top" Margin="20,20,0,0" FontSize="20" FontWeight="Bold" HorizontalAlignment="Left"></Label>
<ComboBox x:Name="cmbCategoria" Margin="0,20,0,0" Height="30" Width="202" FontSize="16" FontWeight="Bold" VerticalAlignment="Top" SelectionChanged="cmbCategoria_SelectionChanged"/>
<Label Content="Tipo de Familia:" Width="165" VerticalAlignment="Top" Margin="20,20,0,0" FontSize="20" FontWeight="Bold"></Label>
<Image x:Name="cmbImagen" >

</Image>
<ComboBox x:Name="cmbFamilias" Margin="0,20,0,0" Height="30" Width="170" VerticalAlignment="Top" 
</WrapPanel>
</Border>
</ComboBoxItem>

<Button Content="Colocar Ejemplares" Grid.Row="2" FontSize="20" FontWeight="Bold"/>

</Grid>
</Window>

 

private void cmbFamilias_SelectionChanged(object sender, SelectionChangedEventArgs e)
{

string nombre = this.cmbFamilias.SelectedItem.ToString();
List<FamilySymbol> Tipos = UtilRevit.ObtenerListaTiposFamiliySymbol(_doc);
FamilySymbol sym = Tipos.First(x => x.Family.Name + ":" + x.Name == nombre);

Size imgSize = new Size(200, 200);
this.cmbImagen.Source=sym.GetPreviewImage(imgSize);

}

0 Likes
Message 4 of 4

reylorente1
Collaborator
Collaborator

Ya obtuve la solución, con estés código

 

//Convertir Bitmap a ImagenSource
private BitmapImage BitmapToImageSource(Bitmap bitmap)
{
using (MemoryStream memory = new MemoryStream())
{
bitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Bmp);
memory.Position = 0;
BitmapImage bitmapimage = new BitmapImage();
bitmapimage.BeginInit();
bitmapimage.StreamSource = memory;
bitmapimage.CacheOption = BitmapCacheOption.OnLoad;
bitmapimage.EndInit();
return bitmapimage;
}
}