[英]ComboBox.SelectedValue returns ToString() instead of the Name property
I am trying to make a ComboBox
where the drop-down list shows the name and description of the campaign elements (properties NombreCampana
and Descripcion
of class Campana
) and when you select one value it shows the Name ( NombreCampana
). 我已經使用ObservableCollection
完成了此操作,因此更改會影響ComboBox
而無需重新加載。
在 MainMenu window ( menuPrincipal
)中,我有這個 XAML 用於ComboBox
:
<ComboBox Style="{DynamicResource comboBoxCampaign}"
x:Name="campaignComboBox"
Text="{DynamicResource CampaignList}"
ItemsSource="{Binding Path=Nombres}"
SelectedValue="{Binding Path=NombreCampana, Mode=TwoWay}"
SelectedValuePath="Value"
SelectionChanged="CampaigneComboBox_SelectionChanged">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0}
{1}">
<Binding Path="NombreCampana"/>
<Binding Path="Descripcion"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
ComboBox
鏈接到代碼隱藏中的ObservableCollection
:
private List<Campana> listacampana = new List<Campana>();
private ObservableCollection<Campana> nombres = new ObservableCollection<Campana>();
public ObservableCollection<Campana> Nombres
{
get { return nombres; }
set { nombres = value; }
}
public List<Campana> ListaCampanas
{
get { return listacampana; }
set { listacampana = value; }
}
public menuPrincipal()
{
InitializeComponent();
ConfiguracionPagina.DefinirIdioma(this, "MainMenu");
this.listacampana.AddRange(MainWindow.DatosUsuario.ListCampaigns);
foreach (Campana item in this.listacampana)
{
this.nombres.Add(item);
}
campaignComboBox.ItemsSource = this.nombres;
}
該代碼定義語言,更改文本,將用戶列表添加到屬性列表,然后將所有項目Campana
(campaign) 添加到ObservableCollection
。
之后,我將ItemSource
的ComboBox
設置為Nombres
( ObservableCollection
)。
class Campana
是:
public class Campana
{
private string nombre;
private string descripcion;
private string imagen;
private List<RecursosCampana> listarecursos = new List<RecursosCampana>();
public string DireccionImagen
{
get { return imagen; }
set { imagen = value; }
}
public List<RecursosCampana> RecursosCampana
{
get { return listarecursos; }
set { listarecursos = value; }
}
public string Descripcion
{
get { return descripcion; }
set { descripcion = value; }
}
public string NombreCampana
{
get { return nombre; }
set { nombre = value; }
}
}
當我運行應用程序時, DataTemplate
工作正常並顯示帶有描述的項目。
我已經嘗試了通過搜索谷歌創建的所有東西,但我不知道發生了什么。 另外,如果我沒有在代碼中定義ItemSource
,則 XAML ItemSource
不起作用。 如果我注釋掉campaignComboBox.ItemsSource = this.nombres;
就會發生這種情況 :
編輯:回答評論,感謝@Keith Stein,我已經解決了,這是問題的一部分,這就是風格
<Style x:Key="comboBoxCampaign" TargetType="ComboBox">
<Setter Property="Margin" Value="10"/>
<Setter Property="BorderThickness" Value="2"/>
<Setter Property="IsEditable" Value="True"/>
<Setter Property="IsReadOnly" Value="True"/>
<Setter Property="Focusable" Value="True"/>
<Setter Property="FontSize" Value="15"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="Height" Value="Auto"/>
</Style>
我用它有一個“占位符”文本,一個初始文本,一旦你點擊 combobox,就會消失,當我刪除“IsEditable”時,我丟失了初始標題 combobox 只需按下 V,但現在當我按下select 一個項目,我得到名稱+描述。
不完全是我想要的,我只是想顯示名字,但更接近那個。
Edit2:數據綁定的問題是我沒有使用 DataContext = this; 現在我可以從 xaml 獲得綁定,但我仍然不能只顯示名稱。
最后回答最后一條評論,我想做的是: - 我正在做一個應用程序來准備角色扮演冒險或場景,在本節中,用戶 select 他將要使用的活動,所以我希望當他按combobox,他可以看到活動的名稱和描述,因為他可以有2個類似的活動或基於同一系統,一旦他選擇一個,如果他寫一個大描述,就顯示更優雅的名稱
最后我在這篇文章中得到了答案我可以對 WPF ComboBox 中的選定項目使用與下拉部分中的項目不同的模板嗎?
如果項目被選中,兩個模板之間的簡單交換。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.