使用列表框更改按钮颜色

时间:2019-05-27 08:37:37

标签: c# wpf xaml

我需要使用ListBox更改Button的背景颜色。我有3种ListBoxItems用于3种颜色(绿色,黄色和红色)。我不确定该怎么做。任何帮助将不胜感激。

<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>

5 个答案:

答案 0 :(得分:2)

这是不使用任何代码隐藏的解决方案。它与列表框绑定。在没有弹出窗口的情况下显示效果更好,因为您必须从按钮上移开焦点才能看到弹出窗口的更改。

   <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>

删除<Popup IsOpen="{Binding IsChecked, ElementName=button}" StaysOpen="False"></Popup>行,这会将列表框直接放在按钮下方。这将立即显示颜色变化。

答案 1 :(得分:1)

为您的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>  

然后在后端,您可以执行以下操作(已通过测试)

private void MyListBx_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
    string SelectedColor = (myListBx.SelectedItem as ListBoxItem).Content.ToString();

    switch (SelectedColor)
    {
        case "Yellow":
            button.Background = Brushes.Yellow;
            break;
        case "Green":
            button.Background = Brushes.Green;
            break;
        case "Pink":
            button.Background = Brushes.Pink;
            break;
    }
}

答案 2 :(得分:1)

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

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中犯了一个错误,因为您无法直接更改切换按钮的背景。下面是如何将转换器与实际代码一起使用。请注意,它只会更改文本块的背景。使按钮看起来更好Please see here

<StackPanel>
    <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!" Background="{Binding Source={x:Reference ColorPicker}, Path=SelectedValue, Converter={StaticResource StringToColorConverter}}"/>
    </ToggleButton>
</StackPanel>

答案 3 :(得分:0)

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

using (StreamReader reader = new StreamReader(HttpResponseStream))
                    {
                        //Response.Code = 1;
                        string body = reader.ReadToEnd();
                        consoleoutput("REST: result" + body);

                        resultdict = JsonConvert.DeserializeObject<Dictionary<string, string>>(body);



                    }

答案 4 :(得分:0)

您还可以使用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>