MouseFver与WPF中的ComboBox项目图像一起出现问题

时间:2014-07-22 03:45:04

标签: c# wpf combobox

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

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

我已在ComboBox项目的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>

提前致谢

1 个答案:

答案 0 :(得分:0)

实际上,您正在使用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;

        }
    }

<强>视图模型:

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