你能在 silverlight 中为 listboxitem 背景设置渐变画笔吗?

发布于 2024-09-04 01:04:47 字数 2081 浏览 2 评论 0原文

我正在寻找一种将渐变画笔设置为列表框项目背景的方法。我定义了一个 DataTemplate 并指定了渐变画笔,但它始终显示为列表框背景(即它从不显示为渐变画笔)。

我已经能够设置列表框本身的背景,并且可以使用“setter”对象将列表框项的背景设置为标准颜色......但这些都不是我所追求的。

我真的希望每个列表项上的背景都是渐变画笔。

下面是我构建的数据模板。

<ListBox Name="MyListBox"  Margin="12,67,12,169">

        <ListBox.ItemTemplate>
        <DataTemplate>
                <Grid Height="51"  VerticalAlignment="Bottom">
                    <Grid.Background>
                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                            <GradientStop Color="#FFC9F4D0"/>
                            <GradientStop Color="#FF2AC12A" Offset="0.333"/>
                            <GradientStop Color="#FF35DE35" Offset="1"/>
                        </LinearGradientBrush>
                    </Grid.Background>
                    <Canvas >
                        <dataInput:Label Width="227" Foreground="Yellow" Canvas.Left="158" Canvas.Top="8" Content="{Binding Place}"/>
                        <dataInput:Label Width="146" Foreground="Yellow" Canvas.Left="8" Canvas.Top="8" Content="{Binding Date}"/>
                        <dataInput:Label Content="{Binding People}" Width="346" FontSize="9.333" Foreground="Black" Canvas.Left="166" Canvas.Top="28"/>
                        <!-- <dataInput:Label Width="45" Content="Accept" Foreground="White" Canvas.Left="8" Canvas.Top="28"/>
                        <dataInput:Label Width="45" Content="Decline" Foreground="White" Canvas.Left="57" Canvas.Top="28"/> -->
                        <dataInput:Label Content="SomeText" Width="101" FontSize="9.333" Foreground="White" Canvas.Left="389" Canvas.Top="10"/>
                        <Image Height="21" Width="21" Canvas.Left="500" Canvas.Top="8" Source="Green Button.png"/>
                    </Canvas>
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

有什么想法吗?

I am looking for a way to set a gradientbrush as the background for a listbox item. I have a DataTemplate defined and have specified a gradient brush but it always appears as the listbox background (i.e. it never shows as a gradient brush).

I have been able to set the background of the listbox itself, and I can set the listboxitem's background to a standard color using the "setter" object....but none of these are what I am after.

I really want the background on each list item to be a gradient brush.

Below is the datatemplate that I have constructed.

<ListBox Name="MyListBox"  Margin="12,67,12,169">

        <ListBox.ItemTemplate>
        <DataTemplate>
                <Grid Height="51"  VerticalAlignment="Bottom">
                    <Grid.Background>
                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                            <GradientStop Color="#FFC9F4D0"/>
                            <GradientStop Color="#FF2AC12A" Offset="0.333"/>
                            <GradientStop Color="#FF35DE35" Offset="1"/>
                        </LinearGradientBrush>
                    </Grid.Background>
                    <Canvas >
                        <dataInput:Label Width="227" Foreground="Yellow" Canvas.Left="158" Canvas.Top="8" Content="{Binding Place}"/>
                        <dataInput:Label Width="146" Foreground="Yellow" Canvas.Left="8" Canvas.Top="8" Content="{Binding Date}"/>
                        <dataInput:Label Content="{Binding People}" Width="346" FontSize="9.333" Foreground="Black" Canvas.Left="166" Canvas.Top="28"/>
                        <!-- <dataInput:Label Width="45" Content="Accept" Foreground="White" Canvas.Left="8" Canvas.Top="28"/>
                        <dataInput:Label Width="45" Content="Decline" Foreground="White" Canvas.Left="57" Canvas.Top="28"/> -->
                        <dataInput:Label Content="SomeText" Width="101" FontSize="9.333" Foreground="White" Canvas.Left="389" Canvas.Top="10"/>
                        <Image Height="21" Width="21" Canvas.Left="500" Canvas.Top="8" Source="Green Button.png"/>
                    </Canvas>
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

Any Thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦初启 2024-09-11 01:04:47

数据模板中发生了什么:

网格的背景被设置为您需要的颜色。然而,在这个网格之上,你的画布正在被绘制。因此线性渐变背景是不可见的。

如何纠正这个问题?

  1. 设置 Canvas.Background={Binding}
  2. 对于您希望继承 Grid.Background 的画布内的任何控件,请设置该控件的 Background={Binding}

示例代码:

-->

                    <Canvas Background="{Binding}">

                        <TextBox Width="227" Canvas.Left="158" Canvas.Top="8" Foreground="Yellow" Text="{Binding Name}" Background="{Binding}"/>
                        <TextBox Width="146" Canvas.Left="8" Canvas.Top="8" Foreground="Yellow" Text="{Binding Language}" Background="{Binding}"/>

                    </Canvas>
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

希望这有帮助!

What is happening in your data template:

The background of the grid is being set to the color you need. However on top of this Grid, your Canvas is getting painted. Hence the linear gradient background is invisible.

How to correct this?

  1. Set Canvas.Background={Binding}
  2. For which ever control within the canvas that you wish to inherit the Grid.Background to, set that control's Background={Binding}

Sample Code:

</Grid.ColumnDefinitions>-->

                    <Canvas Background="{Binding}">

                        <TextBox Width="227" Canvas.Left="158" Canvas.Top="8" Foreground="Yellow" Text="{Binding Name}" Background="{Binding}"/>
                        <TextBox Width="146" Canvas.Left="8" Canvas.Top="8" Foreground="Yellow" Text="{Binding Language}" Background="{Binding}"/>

                    </Canvas>
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

Hope this helps!

狼性发作 2024-09-11 01:04:47

您所做的事情是正确的,它不会设置列表框的背景,它仅设置列表框项的背景。我认为你无法弄清楚。

要弄清楚这一点,只需给网格 5 留出边距,然后看看。您所做的事情是正确的,它不会设置列表框的背景,它仅设置列表框项目的背景。我认为你无法弄清楚。

要弄清楚这一点,只需给你的第 5 格网格留出余量,然后看看。

What you are doing is correct its doesn't sets the background of your listbox, it sets the background of your listboxitem only. I think you couldn't figure it out.

To figure it out just give the margin to u r grid 5 and then see.What you are doing is correct its doesn't sets the background of your listbox, it sets the background of your listboxitem only. I think you couldn't figure it out.

To figure it out just give the margin to u r grid 5 and then see.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文