WPF中通过DataTemplate更改ListBoxItem的前景色

发布于 2024-11-09 01:53:25 字数 1374 浏览 3 评论 0原文

我面临着一种愚蠢的问题:我无法使用 DataTemplate 更改 ListBoxItem 的前景色。我确信有一个非常简单的解决方案,但我只是没有看到它!

这是我的代码:

    <ListBox Height="181" HorizontalAlignment="Left" Margin="12,53,0,0" 
             Name="lstKeys" VerticalAlignment="Top" Width="491">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Margin="3">
                    <DockPanel>
                        <Image Width="32" Height="32" Stretch="Fill" Margin="3"
                               HorizontalAlignment="Left"  Name="image3" VerticalAlignment="Top"
                               Source="{Binding Icon}" />
                        <DockPanel Margin="3">
                            <TextBlock Text="{Binding Product}" Foreground="Green"
                                       DockPanel.Dock="Top" />
                            <TextBlock Text="{Binding Key}" Foreground="Black"
                                       FontWeight="Bold" DockPanel.Dock="Bottom" />
                        </DockPanel>
                    </DockPanel>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

在运行时,颜色是默认的系统颜色。直接通过 ListBox-Attribute 设置前景色不起作用,我希望有人可以提供帮助。

提前致谢!

i'm facing a kindof stupid problem: i'm not able to change the Foreground color of a ListBoxItem with a DataTemplate. I'm sure theres a really simple solution for this but i just don't see it!

Here is my code:

    <ListBox Height="181" HorizontalAlignment="Left" Margin="12,53,0,0" 
             Name="lstKeys" VerticalAlignment="Top" Width="491">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Margin="3">
                    <DockPanel>
                        <Image Width="32" Height="32" Stretch="Fill" Margin="3"
                               HorizontalAlignment="Left"  Name="image3" VerticalAlignment="Top"
                               Source="{Binding Icon}" />
                        <DockPanel Margin="3">
                            <TextBlock Text="{Binding Product}" Foreground="Green"
                                       DockPanel.Dock="Top" />
                            <TextBlock Text="{Binding Key}" Foreground="Black"
                                       FontWeight="Bold" DockPanel.Dock="Bottom" />
                        </DockPanel>
                    </DockPanel>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

At runtime the color ist the default System Color. Setting the Foreground Color directly by ListBox-Attribute doesn't work, i hope somebody can help.

Thanks in advance!

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

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

发布评论

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

评论(1

╰つ倒转 2024-11-16 01:53:25

你好,如果我使用你的列表框,它的工作就像你想要的:)为了知道前景色正在工作,我将数据模板更改为文本块中的固定字符串。

您的“绿色”文本块显示任何文本吗?

<ListBox ItemsSource="{Binding Items}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Margin="3">
                <DockPanel>
                    <Image Width="32" Height="32" Stretch="Fill" Margin="3"
                           HorizontalAlignment="Left"  Name="image3" VerticalAlignment="Top"
                           Source="{Binding Icon}" />
                    <DockPanel Margin="3">
                        <TextBlock Text="the binding seems to be wrong" Foreground="Green"
                                   DockPanel.Dock="Top" />
                        <TextBlock Text="{Binding Key}" Foreground="Black"
                                   FontWeight="Bold" DockPanel.Dock="Bottom" />
                    </DockPanel>
                </DockPanel>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

编辑:我知道这个问题:)

我重写 SystemColors 以获得我想要的。

<ListBox>
  <ListBox.Resources>
     <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="LightBlue"/>
  </ListBox.Resources>
 </ListBox>

hi if i use your listbox its work like you want :) To know that the forecolor is working i changed the datatemplate to a fixed string in the textblock.

does your "green" Textblock show any text?

<ListBox ItemsSource="{Binding Items}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Margin="3">
                <DockPanel>
                    <Image Width="32" Height="32" Stretch="Fill" Margin="3"
                           HorizontalAlignment="Left"  Name="image3" VerticalAlignment="Top"
                           Source="{Binding Icon}" />
                    <DockPanel Margin="3">
                        <TextBlock Text="the binding seems to be wrong" Foreground="Green"
                                   DockPanel.Dock="Top" />
                        <TextBlock Text="{Binding Key}" Foreground="Black"
                                   FontWeight="Bold" DockPanel.Dock="Bottom" />
                    </DockPanel>
                </DockPanel>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

EDIT: i know that problem :)

i override the SystemColors to get what i want.

<ListBox>
  <ListBox.Resources>
     <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="LightBlue"/>
  </ListBox.Resources>
 </ListBox>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文