2008-10-29 51 views
1

我已經重寫了窗口列表框,以便在每個ListBoxItem中顯示圖像和一段文本,但我需要過濾顯示的文本內容我希望通過訪問實際列表框的DisplayMemberPath來實現此目的,但是我無法讓它工作。如何在嘗試將列表框綁定到ListBoxItem的內容時從ListBox訪問DisplayMemberPath?

<Setter Property="ItemContainerStyle"> 
     <Setter.Value> 
      <!-- Simple ListBoxItem - This is used for each Item in a ListBox. The item's content is placed in the ContentPresenter --> 
      <Style TargetType="{x:Type ListBoxItem}"> 
       <Setter Property="SnapsToDevicePixels" Value="true"/> 
       <Setter Property="OverridesDefaultStyle" Value="true"/> 
       <Setter Property="VerticalContentAlignment" Value="Center"/> 
       <Setter Property="Template"> 
        <Setter.Value> 
         <ControlTemplate TargetType="{x:Type ListBoxItem}"> 
          <Grid SnapsToDevicePixels="true"> 
           <Border x:Name="Border"> 
            <Grid Height="40"> 
             <Grid.ColumnDefinitions> 
              <ColumnDefinition Width="Auto"/> 
              <ColumnDefinition Width="*"/> 
             </Grid.ColumnDefinitions> 
             <Image 
              Source="{Binding Path=ThumbnailImage}" 
              Height="30" 
              Width="30" 
              Grid.Column="0"/> 

             <Label 
              x:Name="Text" 
              Content="{TemplateBinding DisplayMemberPath}" 
              VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
              VerticalContentAlignment="Center" 
              HorizontalAlignment="Stretch" 
              Grid.Column="1" 
              Height="40"/> 
            </Grid> 
           </Border> 
          </Grid> 
          <ControlTemplate.Triggers> 
           <Trigger Property="IsSelected" Value="true"> 
            <Setter Property="FontWeight" Value="Bold" TargetName="Text"/> 
            <Setter Property="Foreground" Value="White" TargetName="Text"/> 
            <Setter Property="Background" Value="Blue" TargetName="Border"/> 
           </Trigger> 
          </ControlTemplate.Triggers> 
         </ControlTemplate> 
        </Setter.Value> 
       </Setter> 
      </Style> 
     </Setter.Value> 
    </Setter> 

這是我使用了我的風格的代碼,這是我不能獲得工作線:

CONTENT = 「{TemplateBinding的DisplayMemberPath}」

它與抱怨: 找不到'ListBoxItem'類型的靜態成員'DisplayMemberPathProperty'

任何人都可以指出我正確的方向嗎?

回答

0

它確定,我已經從ListBox中獲得了值,我需要做的就是將其轉換爲從itemssource中的dataobject所需的屬性。

以防萬一有人想知道代碼:

Content="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ListBox}, Path=DisplayMemberPath}" 
相關問題