【发布时间】:2017-08-16 12:58:34
【问题描述】:
当我将图标添加到 ComboBox.ItemTemplate 时,我遇到了一种奇怪的行为。 最初加载项目后,所有项目都显示了相应的图标,但是当我选择一个项目时,图标会显示在 Combo Part 中,但会在 Expander Part 中消失。
You can see the problem here (imgur)
我在 WPF/C# 方面没有经验,我绑定项目或 ComboBox.ItemTemplate 的方式有问题吗?
非常感谢您的帮助。-
XAML 代码
<ComboBox x:Name="comboBox" HorizontalAlignment="Left" Height="32" Margin="60,47,0,0" VerticalAlignment="Top" Width="282" ItemsSource="{Binding OtherTasks}" >
<ComboBox.ItemTemplate>
<DataTemplate>
<DockPanel Margin="0" Height="30">
<ContentControl Content="{Binding Path=Icono}" Margin="0,4,0,0" Background="Yellow" Width="16" Height="16" Visibility="Visible"/>
<AccessText HorizontalAlignment="Stretch" Margin="3,4,0,0" Text="{Binding Path=Text}" TextAlignment="Left" Width="Auto" />
</DockPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
CS 代码
namespace test___icon {
public partial class MainWindow : MetroWindow {
public class CLASS_OTHERTASKS {
public string Text { get; set; }
public object Icono { get; set; }
}
public List<CLASS_OTHERTASKS> OtherTasks { get; set; }
public MainWindow() {
OtherTasks = new List<CLASS_OTHERTASKS>();
OtherTasks.Add(new CLASS_OTHERTASKS() { Text = "Test Air", Icono = new PackIconEntypo() { Kind = PackIconEntypoKind.Air } });
OtherTasks.Add(new CLASS_OTHERTASKS() { Text = "Test Account", Icono = new PackIconMaterial() { Kind = PackIconMaterialKind.Account } });
OtherTasks.Add(new CLASS_OTHERTASKS() { Text = "Test AxisThree", Icono = new PackIconModern() { Kind = PackIconModernKind.AxisThree } });
InitializeComponent();
this.DataContext = this;
}
}
}
【问题讨论】:
标签: wpf combobox binding mahapps.metro