其實你正在設置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();
}
安置自己的XAML代碼! – Sajeetharan