Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to fill a list box with rectangles for each color in Colors in the easyiest possible way

What I have tried:

<pre><Window x:Class="ColorListBox.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
    </Window.Resources>
    <Grid>
        <StackPanel Orientation="Vertical">
            <ListBox ItemsSource="{Colors}" Height="100">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Rectangle Width="100" Height="50" Fill="{Binding}" />
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </StackPanel>
    </Grid>
</Window>

this doesnt work
Posted
Updated 6-Jun-21 17:31pm
Comments
[no name] 6-Jun-21 21:38pm    
Iterate over Colors and create a Rectangle for each color and add to a Grid, StackPanel, WrapPanel, UniformGrid, etc.

1 solution

 
Share this answer
 
Comments
FatalError0x4c 7-Jun-21 16:32pm    
How do i get the Color value to print aside the color name?
[no name] 8-Jun-21 12:30pm    
Need to read the post. Further on:

https://stuff.seans.com/2011/02/14/creating-a-listbox-that-shows-all-predefined-wpf-colors/

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900