簡體   English   中英

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

[英]Change the Forecolor of the ListBoxItem through DataTemplate in WPF

我面臨一種愚蠢的問題:我無法使用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設置前景顏色不起作用,我希望有人可以提供幫助。

提前致謝!

您好,如果我使用您的列表框,它的工作就像您想要的一樣:)要知道前色正在工作,我將datatemplate更改為文本塊中的固定字符串。

您的“綠色”文本塊顯示任何文本嗎?

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM