简体   繁体   English

如何在 WPF listview 中绑定和显示一组颜色?

[英]How to bind and display a set of colors in WPF listview?

Im struggling with a problem that might seem easy first: I want to display some grid with different colors.我首先要解决一个看起来很容易的问题:我想显示一些不同颜色的网格。 I have a listview that is binded to a list.我有一个绑定到列表的列表视图。 The list containst Colors (i have tried SolidColorBrush as well).该列表包含颜色(我也尝试过 SolidColorBrush)。 The listview can display the elements, so in the current case you can see 1 grid per item.列表视图可以显示元素,因此在当前情况下,您可以看到每个项目 1 个网格。 I want to bind the background of the grid (so the datatemplate) to the color property itself.我想将网格的背景(所以数据模板)绑定到颜色属性本身。 For example: lets say i have a white and black color in my list.例如:假设我的列表中有白色和黑色。 Then i want to display a black and a white grid using listview.然后我想使用listview显示一个黑白网格。 However, i cannot bind the background to anything, the binding always fails and I couldnt find a solution.但是,我无法将背景绑定到任何东西,绑定总是失败,我找不到解决方案。

Here is the xaml code:这是xaml代码:

 <ListView ItemsSource="{Binding lightColors}" Height="30"  HorizontalAlignment="Left">
            <ListView.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                    <Setter Property="Margin" Value="5"></Setter>
                    <Setter Property="Background" Value="{Binding **WHAT TO WRITE HERE?**}"></Setter>
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="ListViewItem">

                                <Grid  Height="30" Width="30"></Grid>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </ListView.ItemContainerStyle>
            <ListView.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel>

                    </WrapPanel>
                </ItemsPanelTemplate>

            </ListView.ItemsPanel>
            <ListView.ItemTemplate>
                <DataTemplate>
                
                </DataTemplate>
            </ListView.ItemTemplate>

And some code snippets:还有一些代码片段:

 public List<Color> lightColors { get; set; }

  public void fillLightColors()
    {
        Color myColor = Color.FromRgb(100, 150, 75);
        LightColor.Add(myColor);
      
    }

Currently i cant see anything whenever i try to bind to the background.目前,每当我尝试绑定到背景时,我都看不到任何东西。 Maybe im missing something obvious, maybe i have to use something totally else.也许我错过了一些明显的东西,也许我必须使用完全不同的东西。 Any help would be appriciated!任何帮助将不胜感激!

You would use a SolidColorBrush as value of the Setter:您将使用 SolidColorBrush 作为 Setter 的值:

<Setter Property="Background"> 
    <Setter.Value> 
        <SolidColorBrush Color="{Binding}"/>
    </Setter.Value>
</Setter>

Besides that, the ControlTemplate must also use the Background property:除此之外,ControlTemplate 还必须使用 Background 属性:

<ControlTemplate TargetType="ListViewItem">
    <Grid Height="30" Width="30"
          Background="{TemplateBinding Background}"/>
</ControlTemplate>

Also note that when you set the ListViewItem's Template like this, the ItemTemplate is not used at all.另请注意,当您像这样设置 ListViewItem 的模板时,根本不会使用ItemTemplate The ControlTemplate should have a ContentPresenter . ControlTemplate 应该有一个ContentPresenter

<ControlTemplate TargetType="ListViewItem">
    <Grid Height="30" Width="30"
          Background="{TemplateBinding Background}">
        <ContentPresenter/>
    </Grid>
</ControlTemplate>

How about this:这个怎么样:

Style:风格:

<Style x:Key="ColorComboStyle" TargetType="{x:Type ComboBox}">
    <Setter Property="ItemsSource" Value="{Binding Path=(global:GlobalVars.AllBrushes)}"/>
    <Setter Property="SelectedValuePath" Value="Name"/>
    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
    <Setter Property="ItemTemplate">
        <Setter.Value>
            <DataTemplate>
                <Grid HorizontalAlignment="Stretch" Height="24">
                    <DockPanel  LastChildFill="True" HorizontalAlignment="Stretch">
                        <Rectangle DockPanel.Dock="Right" Fill="{Binding}" Stroke="Black" StrokeThickness="1" Height="20" Width="20"/>
                        <Label Padding="2" Content="{Binding}" VerticalAlignment="Center" HorizontalAlignment="Left"/>
                    </DockPanel>
                </Grid>
            </DataTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="ItemContainerStyle">
        <Setter.Value>
            <Style TargetType="{x:Type ComboBoxItem}">
                <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
            </Style>
        </Setter.Value>
    </Setter>
</Style>

The GlobarVars property looks like this: GlobarVars 属性如下所示:

public static List<string> AllBrushes { get; } = typeof(Brushes).GetProperties().Select(x => x.Name).ToList();

Then use a ComboBox like this:然后使用这样的组合框:

<ComboBox Style="{StaticResource ColorComboStyle}" SelectedValue="{Binding ...}"/>

Worked very well in my case... You will just need to adapt the code to work in your ListView.在我的情况下工作得很好......您只需要调整代码以在您的 ListView 中工作。

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

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