2012-05-29 50 views
0

我有一個類ExpanderItems,它在運行時加載並且其列表設置爲ListBoxDataContext。現在我想要做的是將每個項目的相應圖像顯示爲Tooltip。任何建議如何做到這一點?在代碼中爲列表框項目設置工具提示圖像

public class ExpanderItem 
{ 
    private String mItemName = "empty"; 
    public String ItemName 
    { 
     get { return mItemName; } 
     set { mItemName = value; } 
    } 

    private Image mItemSymbol = null; 
    public Image ItemSymbol 
    { 
     get { return mItemSymbol; } 
     set { mItemSymbol = value; } 
    } 
} 

public List<ExpanderItem> getExpanderItems() 
    { 
     List<ExpanderItem> ItemList = new List<ExpanderItem>(); 

     ExpanderItem i0 = new ExpanderItem(); 
     i0.ItemName = "Constant"; 
     i0.ItemSymbol = new Image(); 
     BitmapImage bi = new BitmapImage(); 
     bi.BeginInit(); 
     bi.UriSource = new Uri(@"/resources/Constant.png", UriKind.RelativeOrAbsolute); 
     bi.EndInit(); 
     i0.ItemSymbol.Source = bi; 
     ItemList.Add(i0); 
     ... 
    } 

在窗前,項目用於我打電話:

void WindowMain_Loaded(object sender, RoutedEventArgs e) 
    { 
     lbItems.DataContext = SomeService.getExpanderItems(); 
    } 

XAML看起來像:

<ListBox x:Name="lstItems" ItemsSource="{Binding}"> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <Label Content="{Binding ItemName}"> 
       </Label> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 

回答

3

編譯和測試解決方案。

XAML:

<ListBox x:Name="lb" 
     ItemsSource="{Binding}" 
     HorizontalAlignment="Stretch" 
     VerticalAlignment="Stretch"> 
    <ListBox.ItemContainerStyle> 
     <Style TargetType="{x:Type ListBoxItem}"> 
      <Setter Property="ToolTip"> 
       <Setter.Value> 
        <Image Stretch="UniformToFill" 
          Source="{Binding ItemSymbol}" /> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </ListBox.ItemContainerStyle> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <Label Grid.Column="1" 
        Content="{Binding ItemName}"> 
      </Label> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

代碼:

using System; 
using System.Collections.Generic; 
using System.Windows; 
using System.Windows.Media.Imaging; 

namespace Test 
{ 
    public class ExpanderItem 
    { 
     private String mItemName = "empty"; 
     public String ItemName 
     { 
      get { return mItemName; } 
      set { mItemName = value; } 
     } 

     private BitmapImage mItemSymbol = null; 
     public BitmapImage ItemSymbol 
     { 
      get { return mItemSymbol; } 
      set { mItemSymbol = value; } 
     } 
    } 

    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      this.InitializeComponent(); 
      lb.DataContext = this.getExpanderItems(); 
     } 

     public List<ExpanderItem> getExpanderItems() 
     { 
      List<ExpanderItem> ItemList = new List<ExpanderItem>(); 
      ExpanderItem i0 = new ExpanderItem 
      { 
       ItemName = "Constant", 
       ItemSymbol = new BitmapImage(new Uri(@"/resources/constant.png", UriKind.RelativeOrAbsolute)) 
      }; 
      ItemList.Add(i0); 

      ExpanderItem i1 = new ExpanderItem 
      { 
       ItemName = "Constant", 
       ItemSymbol = new BitmapImage(new Uri(@"/resources/constant.png", UriKind.RelativeOrAbsolute)) 
      }; 
      ItemList.Add(i1); 

      return ItemList; 
     } 
    } 
} 
+0

這將導致以下錯誤:無法添加類型 'System.Windows.Controls.Image' 的內容類型的對象System.Object的「。標記文件中對象'System.Windows.Controls.Image'的錯誤... – metacircle

+0

這是因爲您將System.Windows.Controls.Image設置爲另一個System.Windows.Controls.Image控件的源。這是無法完成的。 ItemSybol屬性應該是BitmapImage。沒注意到,對不起。 – dodsky

+0

其實我不確定這是否是這裏的問題:您的新解決方案導致:無法將類型'System.Windows.Controls.Border'的內容添加到'System.Object'類型的對象。標記文件 – metacircle

0

我發現了這樣一個解決方案,但這並不工具提示添加到列表框整體項目,但只是爲了它的文本。

public class ExpanderItem 
{ 
    private String mItemName = "empty"; 
    public String ItemName 
    { 
     get { return mItemName; } 
     set { mItemName = value; } 
    } 

    private Image mItemSymbol = null; 
    public Image ItemSymbol 
    { 
     get { return mItemSymbol; } 
     set { mItemSymbol = value; } 
    } 
} 


public List<ExpanderItem> getExpanderItems() 
{ 
    List<ExpanderItem> ItemList = new List<ExpanderItem>(); 

    ExpanderItem i0 = new ExpanderItem(); 
    i0.ItemName = "Constant"; 
    i0.ItemSymbol = new Uri(@"/resources/Constant.png", UriKind.RelativeOrAbsolute); 
    ItemList.Add(i0); 
    ... 
} 

XAML:

<ListBox.ItemTemplate> 
    <DataTemplate> 
    ... 
    <Label Grid.Column="1" Content="{Binding ItemName}"> 
     <Label.ToolTip> 
     <Border BorderBrush="Black" 
       BorderThickness="1" 
       CornerRadius="2"> 
      <Image Source="{Binding ItemSymbol}" /> 
     </Border> 
     </Label.ToolTip> 
    </Label> 
相關問題