C# 使用列表框更改按钮颜色

C# 使用列表框更改按钮颜色,c#,wpf,xaml,C#,Wpf,Xaml,我需要使用ListBox更改按钮的背景色。我有3种颜色(绿色、黄色和红色)的3个ListBoxItem。我不知道该怎么做。任何帮助都将不胜感激 <StackPanel Grid.Column="1" Grid.Row="3"> <ToggleButton Height="50" Content="Change!" Name="button"> </ToggleButton> <Popup IsOpen="{Binding IsChec

我需要使用ListBox更改按钮的背景色。我有3种颜色(绿色、黄色和红色)的3个ListBoxItem。我不知道该怎么做。任何帮助都将不胜感激

<StackPanel Grid.Column="1" Grid.Row="3">
  <ToggleButton Height="50" Content="Change!" Name="button">
   </ToggleButton>
    <Popup  IsOpen="{Binding IsChecked, ElementName=button}"   StaysOpen="False">
     <Border Background="LightYellow">
       <ListBox>
        <ListBoxItem>Green</ListBoxItem>
         <ListBoxItem>Yellow</ListBoxItem>
        <ListBoxItem>Red</ListBoxItem>
      </ListBox>    
    </Border>
  </Popup>
</StackPanel>

绿色
黄色的
红色

为列表框命名并附加事件处理程序
SelectionChanged

<ToggleButton Height="50" Content="Change!" Name="button">
<ListBox x:Name="myListBx" SelectionChanged="MyListBx_OnSelectionChanged">
    <ListBoxItem>Green</ListBoxItem>
    <ListBoxItem>Yellow</ListBoxItem>
    <ListBoxItem>Red</ListBoxItem>
</ListBox>  

为列表框指定名称并附加事件处理程序
SelectionChanged

<ToggleButton Height="50" Content="Change!" Name="button">
<ListBox x:Name="myListBx" SelectionChanged="MyListBx_OnSelectionChanged">
    <ListBoxItem>Green</ListBoxItem>
    <ListBoxItem>Yellow</ListBoxItem>
    <ListBoxItem>Red</ListBoxItem>
</ListBox>  

在ListBox控件中使用SelectionChanged事件处理程序,如下所示

    private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        var listBoxItem = ((sender as ListBox).SelectedItem as ListBoxItem);
        button.Background = (SolidColorBrush)new BrushConverter().ConvertFromString(listBoxItem.Content.ToString());
    }

在ListBox控件中使用SelectionChanged事件处理程序,如下所示

    private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        var listBoxItem = ((sender as ListBox).SelectedItem as ListBoxItem);
        button.Background = (SolidColorBrush)new BrushConverter().ConvertFromString(listBoxItem.Content.ToString());
    }

这是我使用转换器的解决方案。转换器将接收字符串值,然后返回颜色,见下文

public class StringToColorConverter : IValueConverter
{

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var listItem = value as ListBoxItem;
        if (listItem != null)
        {
            var text = listItem.Content;
            switch (text)
            {
                case "Green":
                    return new SolidColorBrush(Colors.Green);
                case "Yellow":
                    return new SolidColorBrush(Colors.Yellow);
                case "Red":
                    return new SolidColorBrush(Colors.Red);
            }
        }
        return new SolidColorBrush(Colors.Transparent);
    }

    public object ConvertBack(object value, Type targetType,
        object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
您在xaml中犯了一个错误,因为无法直接更改切换按钮的背景。下面是如何在实际代码中使用转换器。请注意,它只会更改文本块的背景。使按钮看起来更好


绿色
黄色的
红色

这是我使用转换器的解决方案。转换器将接收字符串值,然后返回颜色,见下文

public class StringToColorConverter : IValueConverter
{

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var listItem = value as ListBoxItem;
        if (listItem != null)
        {
            var text = listItem.Content;
            switch (text)
            {
                case "Green":
                    return new SolidColorBrush(Colors.Green);
                case "Yellow":
                    return new SolidColorBrush(Colors.Yellow);
                case "Red":
                    return new SolidColorBrush(Colors.Red);
            }
        }
        return new SolidColorBrush(Colors.Transparent);
    }

    public object ConvertBack(object value, Type targetType,
        object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
您在xaml中犯了一个错误,因为无法直接更改切换按钮的背景。下面是如何在实际代码中使用转换器。请注意,它只会更改文本块的背景。使按钮看起来更好


绿色
黄色的
红色

这是一个不使用代码隐藏的解决方案。它与列表框绑定。如果没有弹出窗口,它会显示得更好,因为您必须从按钮上移除焦点才能看到弹出窗口的变化

   <StackPanel Grid.Column="1" Grid.Row="3">
        <ToggleButton  Height="50" Content="Change!" Name="button">
            <ToggleButton.Background>
                <Binding ElementName="lb" Path="SelectedItem.Content" />
            </ToggleButton.Background>
        </ToggleButton>
        <Popup  IsOpen="{Binding IsChecked, ElementName=button}"   StaysOpen="False">
            <Border Background="LightYellow">
                <ListBox Name="lb" >
                    <ListBoxItem>Green</ListBoxItem>
                    <ListBoxItem>Yellow</ListBoxItem>
                    <ListBoxItem>Red</ListBoxItem>
                </ListBox>
            </Border>
        </Popup>
    </StackPanel>

绿色
黄色的
红色

删除将列表框直接置于按钮下方的
行。这将立即显示颜色变化。

这是一个不使用代码隐藏的解决方案。它与列表框绑定。如果没有弹出窗口,它会显示得更好,因为您必须从按钮上移除焦点才能看到弹出窗口的变化

   <StackPanel Grid.Column="1" Grid.Row="3">
        <ToggleButton  Height="50" Content="Change!" Name="button">
            <ToggleButton.Background>
                <Binding ElementName="lb" Path="SelectedItem.Content" />
            </ToggleButton.Background>
        </ToggleButton>
        <Popup  IsOpen="{Binding IsChecked, ElementName=button}"   StaysOpen="False">
            <Border Background="LightYellow">
                <ListBox Name="lb" >
                    <ListBoxItem>Green</ListBoxItem>
                    <ListBoxItem>Yellow</ListBoxItem>
                    <ListBoxItem>Red</ListBoxItem>
                </ListBox>
            </Border>
        </Popup>
    </StackPanel>

绿色
黄色的
红色

删除将列表框直接置于按钮下方的
行。这将立即显示颜色变化。

您也可以使用DataTrigger执行类似操作

<StackPanel Grid.Row="2">
            <Popup IsOpen="{Binding IsChecked, ElementName=button}"  StaysOpen="False">
                <Border Background="LightYellow">
                    <ListBox x:Name="ColorPicker" >
                        <ListBoxItem>Green</ListBoxItem>
                        <ListBoxItem>Yellow</ListBoxItem>
                        <ListBoxItem>Red</ListBoxItem>
                    </ListBox>
                </Border>
            </Popup>
            <ToggleButton Name="button"
                  Height="50">
                <TextBlock Text="Change!" >
                    <TextBlock.Style>
                        <Style TargetType="TextBlock">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding ElementName=ColorPicker,Path=SelectedIndex}"  Value="0">
                                    <Setter Property="Background" Value="Green"></Setter>
                                </DataTrigger>
                                <DataTrigger Binding="{Binding ElementName=ColorPicker,Path=SelectedItem}"  Value="1">
                                    <Setter Property="Background" Value="Yellow"></Setter>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </TextBlock.Style>

                </TextBlock>

            </ToggleButton>
        </StackPanel>

绿色
黄色的
红色

您也可以使用DataTrigger执行类似操作

<StackPanel Grid.Row="2">
            <Popup IsOpen="{Binding IsChecked, ElementName=button}"  StaysOpen="False">
                <Border Background="LightYellow">
                    <ListBox x:Name="ColorPicker" >
                        <ListBoxItem>Green</ListBoxItem>
                        <ListBoxItem>Yellow</ListBoxItem>
                        <ListBoxItem>Red</ListBoxItem>
                    </ListBox>
                </Border>
            </Popup>
            <ToggleButton Name="button"
                  Height="50">
                <TextBlock Text="Change!" >
                    <TextBlock.Style>
                        <Style TargetType="TextBlock">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding ElementName=ColorPicker,Path=SelectedIndex}"  Value="0">
                                    <Setter Property="Background" Value="Green"></Setter>
                                </DataTrigger>
                                <DataTrigger Binding="{Binding ElementName=ColorPicker,Path=SelectedItem}"  Value="1">
                                    <Setter Property="Background" Value="Yellow"></Setter>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </TextBlock.Style>

                </TextBlock>

            </ToggleButton>
        </StackPanel>

绿色
黄色的
红色