2014-07-22 63 views
0

我有一個ComboBox項目,其中包括圖像和文本。當我跑步時我的應用程序圖像中的鼠標懸停情況下消失在下面的鏈接圖所示:WPF中的ComboBox項目圖像的MouseOver問題

http://postimg.org/image/wcdfgwp7t/f4518372/

我加入的background屬性的組合框項目的形象。在這種情況下,我希望Item顯示我的圖像,我該如何處理?

我的XAML代碼:

<ComboBox x:Name="comboBox1" HorizontalAlignment="Left" Margin="66,150,0,0" VerticalAlignment="Top" Width="84" Text="Valg Enhed"> 
     <ComboBoxItem Content="Stk" FontSize="16" RenderTransformOrigin="0.5,0.5"> 
      <ComboBoxItem.Background> 
       <ImageBrush ImageSource="apple_green.png" Stretch="Uniform"> 
        <ImageBrush.RelativeTransform> 
         <TransformGroup> 
          <ScaleTransform CenterY="0.5" CenterX="0.65" ScaleY="1" ScaleX="-1"/> 
          <SkewTransform AngleY="0" AngleX="0" CenterY="0.5" CenterX="0.65"/> 
          <RotateTransform Angle="0" CenterY="0.5" CenterX="0.65"/> 
          <TranslateTransform/> 
         </TransformGroup> 
        </ImageBrush.RelativeTransform> 
       </ImageBrush> 
      </ComboBoxItem.Background> 
     </ComboBoxItem>    
    </ComboBox> 

在此先感謝

+0

安置自己的XAML代碼! – Sajeetharan

回答

0

其實你正在設置ComboboxItem的與RelativeTransform的背景,那就是這裏造成問題的原因。

你可以簡單地做到這一點,

<ComboBox x:Name="comboBox1" HorizontalAlignment="Left" Margin="66,150,0,0" VerticalAlignment="Top" Width="84" Text="Valg Enhed"> 
      <ComboBoxItem Name="Stk"> 
       <StackPanel Orientation="Horizontal"> 
        <Label Content="Stk " Width="50"/> 
        <Image Source="apple_green.png" Width="16" Height="14" /> 
       </StackPanel> 
      </ComboBoxItem> 
     </ComboBox> 

隨着結合: XAML:

<ComboBox Name="cmb1" SelectedValue="{Binding comboItem}" 
     ItemsSource="{Binding Comboboxitems}" Margin="176,216,0,181" SelectionChanged="cmb1_SelectionChanged"> 
     <ComboBox.ItemTemplate> 
      <DataTemplate> 
       <StackPanel Orientation="Horizontal">      
        <TextBlock Height="20" Text="{Binding Label}" Width="100" /> 
        <Image Height="20" Source="{Binding Image}" Width="100" Stretch="Fill" /> 
       </StackPanel> 
      </DataTemplate> 
     </ComboBox.ItemTemplate> 
</ComboBox> 

型號:

public class comboItem 
    { 
     public ImageSource Image { get; set; } 
     public string Label { get; set; } 


     public comboItem(ImageSource image, string label) 
     { 
      Image = image; 
      Label = label; 

     } 
    } 

視圖模型:

public class ViewModel : INotifyPropertyChanged 
    { 
     public ViewModel() 
     { 
      List<comboItem> list = new List<comboItem>(); 
      list.Add(new comboItem(new BitmapImage(new Uri("/WpfApplication1;component/Images/FT-C2HB.JPEG", UriKind.Relative)), "Stk")); 
      list.Add(new comboItem(new BitmapImage(new Uri("//WpfApplication1//Images//FT-C2HB.JPEG", UriKind.Relative)), "abc")); 
      _comboboxitems = new CollectionView(list); 
     } 
     private readonly CollectionView _comboboxitems; 
     private comboItem _comboitem; 

     public CollectionView Comboboxitems 
     { 
      get { return _comboboxitems; } 
     } 

     public comboItem Comboitem 
     { 
      get { return _comboitem; } 
      set 
      { 
       if (_comboitem == value) return; 
       _comboitem = value; 
       OnPropertyChanged("Comboitem"); 
      } 
     } 
     private void OnPropertyChanged(string propertyName) 
     { 
      if (PropertyChanged != null) 
       PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
     public event PropertyChangedEventHandler PropertyChanged; 
    } 

Mainpage.cs:

public MainWindow() 
     { 
      InitializeComponent(); 
      this.DataContext = new ViewModel(); 
     } 
+0

非常感謝。在這種情況下,如何將comboBox1.text設置爲「Stk」?處理StackPanels時會變成一個問題。 – user3812509