为什么我不能绑定ListBoxItem的前景色?

时间:2014-10-01 23:30:58

标签: c# wpf listbox

我有一个ListBox,它使用Style来定义ListBoxItems的外观。这是风格:


<Style x:Key="OutputListBoxStyle" TargetType="ListBox">
  <Setter Property="TextElement.FontFamily" Value="Consolas" />
  <Setter Property="TextElement.FontSize" Value="12" />
  <Setter Property="VirtualizingStackPanel.IsVirtualizing" Value="True" />
  <Setter Property="VirtualizingStackPanel.VirtualizationMode" Value="Recycling" />
  <Setter Property="hyp:ListBoxSelector.Enabled" Value="True" />
  <Setter Property="ItemContainerStyle" Value="{StaticResource OutputListBoxItemStyle}" />
  <Setter Property="ContextMenu">
    <Setter.Value>
      <ContextMenu>
        <MenuItem Command="Copy" />
      </ContextMenu>
    </Setter.Value>
  </Setter>
</Style>

<Style x:Key="OutputListBoxItemStyle" TargetType="ListBoxItem">
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="ListBoxItem">
        <TextBlock Text="{Binding Text}" TextWrapping="Wrap"
          Background="{TemplateBinding Background}"
          Foreground="{Binding ForegroundColor}"
          FontWeight="{TemplateBinding FontWeight}"/>
        <ControlTemplate.Triggers>
          <Trigger Property="IsSelected" Value="true">
            <Setter Property="Background" Value="Blue" />
            <Setter Property="Foreground" Value="{StaticResource {x:Static SystemColors.HighlightTextBrushKey}}" />
          </Trigger>
        </ControlTemplate.Triggers>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

如您所见,ListBoxItem中TextBlock的Foreground属性绑定到我的对象的ForegroundColor属性。我还尝试使用ListBoxItem本身的Setter而不是TextBlock进行绑定,并让TextBlock像Background属性一样使用TemplateBinding,但这也不起作用。这是我绑定的对象:


public class PrintInfo : ViewModelBase
{
    protected Brush foreColor;

    public String Text { get; set; }
    public Brush ForegroundColor { get { return foreColor; } set { foreColor = value; RaisePropertyChanged("ForegroundColor"); } }

    public PrintInfo()
    {
        ForegroundColor = Brushes.White;
    }

    public PrintInfo(String text) : base()
    {
        Text = text;
    }
}

正在打印文本,但没有前景色,因此不可见。我希望前景色为白色。我已经看到其他SO问题以与我尝试做的非常相似的方式绑定,所以我很困惑为什么这不起作用。这是ListBox本身:


<ListBox Name="OutputBox" Grid.Column="0" ItemsSource="{Binding Output,RelativeSource={RelativeSource FindAncestor,AncestorType=Window}}"
                  ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Visible"
                  Background="{DynamicResource ControlBackgroundBrush}"
                  Style="{StaticResource OutputListBoxStyle}">

编辑1:根据Rohit的要求,在此示例中添加了ListBox的样式。
编辑2:因此,人们不必搜索我的评论,问题不在我的任何XAML中,而是在我的自定义对象PrintInfo中。我正在调用: base()而不是: this(),并且期望调用默认构造函数,当然它不是,正在调用超类构造函数。因此,从未设置前景颜色,因此我的隐形文本会产生。

1 个答案:

答案 0 :(得分:1)

在您的代码中, ListBoxItem样式不会应用于ListBox实例的任何位置 。此外,我没有看到任何需要覆盖ListBoxItem的模板,您可以像这样设置 ItemTemplate

<ListBox Name="OutputBox" Grid.Column="0"
         ItemsSource="{Binding Output, RelativeSource={RelativeSource FindAncestor, 
                                                                AncestorType=Window}}"
         ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
         ScrollViewer.VerticalScrollBarVisibility="Visible"
         Background="{DynamicResource ControlBackgroundBrush}"
         Style="{StaticResource OutputListBoxStyle}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Text}" TextWrapping="Wrap">
                <TextBlock.Style>
                    <Style TargetType="TextBlock">
                        <Setter Property="Foreground" Value="{Binding ForegroundColor}"/>
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType=ListBoxItem}}"
                                         Value="True">
                                <Setter Property="Background" Value="Blue" />
                                <Setter Property="Foreground" Value="{StaticResource {x:Static SystemColors.HighlightTextBrushKey}}" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </TextBlock.Style>
            </TextBlock>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>