WPF中组合框项图像的鼠标悬停问题

本文关键字:鼠标 悬停 问题 图像 组合 WPF | 更新日期: 2023-09-27 18:14:17

我有一个包含图像和文本的ComboBox项目。当我运行我的应用程序时,图像在鼠标悬停情况下消失,如下面的链接图所示:

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

我已经在ComboBox项目的背景属性上添加了图像。在这种情况下,我希望Item显示我的图像,我该如何处理?

My XAML code:

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

Thanks in advance

WPF中组合框项图像的鼠标悬停问题

实际上,您正在使用RelativeTransform设置ComboboxItem的背景,这导致了这里的问题。

你可以简单地这样做,

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

ViewModel:

 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();
        }