Click here to Skip to main content
15,887,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am learning WPF atm, I would like to make a listbox have it's list items color be set by mouse actions. I have number of labels that have a color. On the mouse down event I want to capture the color of the label, and on the mouse up of the Listbox item change the background color. I am using .NET 4.5 WPF in VS 2015. How would I go about this please?

What I have tried:

                <Style TargetType="{x:Type ListBoxItem}">
                    <Setter Property="Background" Value="Silver"/>
                    <Style.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="Background" Value="Red"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </ListBox.Resources>
            <!--</ListBox.Resources>-->
        </ListBox>

        <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Height="29" Margin="55,475,0,0" VerticalAlignment="Top" Width="214" Click="button_Click" Grid.ColumnSpan="2"/>
        <StackPanel HorizontalAlignment="Left" Height="362" Margin="169,76,0,0" VerticalAlignment="Top" Width="79" Grid.Column="1">
            <Label x:Name="label1" MouseDown="label1_MouseDown" Content="" HorizontalAlignment="Left" Height="51" Width="79" Background="Red"/>
            <Label x:Name="label2" MouseDown="label2_MouseDown" Content="" HorizontalAlignment="Left" Height="51" Width="79" Background="Green"/>
            <Label x:Name="label3" MouseDown="label3_MouseDown" Content="" HorizontalAlignment="Left" Height="51" Width="79" Background="Yellow"/>
            <Label x:Name="label4" MouseDown="label4_MouseDown" Content="" HorizontalAlignment="Left" Height="51" Width="79" Background="Blue"/>
            <Label x:Name="label5" MouseDown="label5_MouseDown" Content="" HorizontalAlignment="Left" Height="51" Width="79" Background="Magenta"/>
            <Label x:Name="label6" MouseDown="label6_MouseDown" Content="" HorizontalAlignment="Left" Height="51" Width="79" Background="Cyan"/>
            <Label x:Name="label7" DragOver="label7_DragLeave" DragLeave="label7_DragLeave"  Content="Random" HorizontalAlignment="Left" Height="51" Width="79" Background="White" FontSize="18" BorderThickness="1,0,1,1" BorderBrush="Black"/>
        </StackPanel>

    </Grid>
</Window>
Posted
Updated 8-Apr-20 8:25am

1 solution

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