简体   繁体   English

WPF 滑块更改按钮 colors 点击

[英]WPF sliders to change button colors on click

I am doing a project where I have to change button colors based on sliders in WPF.我正在做一个项目,我必须根据 WPF 中的滑块更改按钮 colors。 This is my XAML design.这是我的 XAML 设计。

XAML file XAML 文件

So, I have made 64 buttons, and I have tried implementing multiple options, and none worked for me.因此,我制作了 64 个按钮,并尝试实现多个选项,但没有一个对我有用。

I have this xaml code:我有这个 xaml 代码:

<Grid Grid.Column="0" Grid.RowSpan="4" Grid.ColumnSpan="4" HorizontalAlignment="Left" Height="790" Margin="0,10,0,0" Grid.Row="0" VerticalAlignment="Top" Width="800">
            <Grid Margin="230,114,230,346" AutomationProperties.Name="ledMatrix" Tag="ledMatrix" >
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>

<Button Grid.Column="0" Grid.Row="0" Height="40" Width="40" Command="{Binding LEDBtn}" Background="#FFAAAAAA" Tag="LED00" Click="changeLedIndicatiorColor" x:Name="LEDButton"/>
                <Button Grid.Column="1" Grid.Row="0" Height="40" Width="40" Command="{Binding LEDBtn}" Background="#FFAAAAAA" Tag="LED01"/>
                <Button Grid.Column="2" Grid.Row="0" Height="40" Width="40" Command="{Binding LEDBtn}" Background="#FFAAAAAA" Tag="LED02"/>
                <Button Grid.Column="3" Grid.Row="0" Height="40" Width="40" Command="{Binding LEDBtn}" Background="#FFAAAAAA" Tag="LED03"/>
                <Button Grid.Column="4" Grid.Row="0" Height="40" Width="40" Command="{Binding LEDBtn}" Background="#FFAAAAAA" Tag="LED04"/>
                <Button Grid.Column="5" Grid.Row="0" Height="40" Width="40" Command="{Binding LEDBtn}" Background="#FFAAAAAA" Tag="LED05"/>
                <Button Grid.Column="6" Grid.Row="0" Height="40" Width="40" Command="{Binding LEDBtn}" Background="#FFAAAAAA" Tag="LED06"/>
                <Button Grid.Column="7" Grid.Row="0" Height="40" Width="40" Command="{Binding LEDBtn}" Background="#FFAAAAAA" Tag="LED07"/>

and sliders:和滑块:

<Grid Margin="230,480,230,170">
                <Slider x:Name="SeekR" Margin="0,0,0,120" Tag="seekR" Maximum="255" Background="Red" ></Slider>
                <Slider x:Name="SeekG" Margin="0,50,0,70" Tag="seekG" Maximum="255" Background="Green"></Slider>
                <Slider x:Name="SeekB" Margin="0,100,0,20" Tag="seekB" Maximum="255" Background="Blue"></Slider>
            </Grid>

I have tried placing this in my xaml.cs file:我试过把它放在我的 xaml.cs 文件中:

public void changeColor()
        {
            byte rr = (byte)SeekR.Value;
            byte gg = (byte)SeekG.Value;
            byte bb = (byte)SeekB.Value;
            Color cc = Color.FromRgb(rr, gg, bb); //Create object of Color class.
            SolidColorBrush colorBrush = new SolidColorBrush(cc); //Creating object of SolidColorBruch class.
            LEDButton.Background = colorBrush; //Setting background of stack panel.
        }
        private void changeLedIndicatiorColor(object sender, RoutedEventArgs e)
        {
            changeColor();
        }

But from this code, I have to name all the buttons differently butI want to have the same binding for all buttons and try to connect it all together, so for example I would like to set color by sliders and when I click the button color would change.但是从这段代码中,我必须以不同的方式命名所有按钮,但我希望所有按钮具有相同的绑定并尝试将它们连接在一起,因此例如我想通过滑块设置颜色,当我单击按钮颜色时改变。 Then, I can change the color, and make other buttons that different color.然后,我可以更改颜色,并制作不同颜色的其他按钮。

Refactor ChangeColor to accept a Button , the button to change the color for:重构ChangeColor以接受Button ,用于更改颜色的按钮:

private void ChangeLedIndicatiorColor_OnClick(object sender, RoutedEventArgs e)
{
  ChangeColor(sender as Button);
}

public void ChangeColor(Button button)
{
  byte rr = (byte)SeekR.Value;
  byte gg = (byte)SeekG.Value;
  byte bb = (byte)SeekB.Value;
  Color cc = Color.FromRgb(rr, gg, bb); 
  SolidColorBrush colorBrush = new SolidColorBrush(cc); 

  button.Background = colorBrush;
}

Then register the ChangeLedIndicatiorColor event handler for each Button.Click :然后为每个Button.Click注册ChangeLedIndicatiorColor事件处理程序:

...

<Button Click="ChangeLedIndicatiorColor" />
<Button Click="ChangeLedIndicatiorColor" />
<Button Click="ChangeLedIndicatiorColor" />
...

You can use the EventSetter so that the event can be applicable to all the buttons.您可以使用 EventSetter 以使事件适用于所有按钮。 You don't need to attach the click event handler to each button anymore.您不再需要将 click 事件处理程序附加到每个按钮。

    <Window.Resources>
        <Style TargetType="Button">
            <EventSetter Event="Click" Handler="ChangeLedIndicatiorColor"/>
        </Style>
    </Window.Resources>

And inside event handler you can change the color of the button which triggered the click event.在事件处理程序中,您可以更改触发点击事件的按钮的颜色。

private void changeLedIndicatiorColor(object sender, RoutedEventArgs e)
{
    byte rr = (byte)SeekR.Value;
    byte gg = (byte)SeekG.Value;
    byte bb = (byte)SeekB.Value;
    Color cc = Color.FromRgb(rr, gg, bb); 
    SolidColorBrush colorBrush = new SolidColorBrush(cc); 
    (sender as Button).Background = colorBrush; 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM