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
). I have done this with an ObservableCollection
so the changes affect the ComboBox
without reloading.
In the MainMenu window (menuPrincipal
), I have this XAML for the 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>
The ComboBox
, is linked to an ObservableCollection
in the code-behind:
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;
}
That code defines the language, changes the texts, adds the user list to the property list, and after that adds to the all the items Campana
(campaign) to the ObservableCollection
.
After that I set the ItemSource
of the ComboBox
to Nombres
(the ObservableCollection
).
The class Campana
is:
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; }
}
}
When I run the application, the DataTemplate
works fine and shows the items with the description.
But when I select one item I get this:
I have tried everything that I founded by searching Google, but I don't get what is happening. Also, if I don't define the ItemSource
in the code, the XAML ItemSource
doesn't work. This is what happens if I comment out campaignComboBox.ItemsSource = this.nombres;
:
Edit: answering the comments, i have solved thanks to @Keith Stein, a part of the problem, this was the style
<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>
I used that to have a "placeholder" text, an initial text which is going to dissapears once you click in the combobox, when i delete "IsEditable" i lost the initial title the combobox just have the V to press, but now when i select an item, i get the name + description.
Is not exactly what i want, i just want to show the name, but is more close to that.
Edit2: The problem with the databinding was that i didnt use the DataContext = this; now i can get the bind from xaml but still i cant show just the name.
Lastly answering to the last comment, what i am trying to do is: - I'm doing an application to prepare roleplaying adventures or scenarios, in this section the user select the campaign that he is going to use, so i want that when he press the combobox, he can see the name of the campaign and the description because he could have 2 similar campaigns or based in the same system, and once he choose one, just show the name that is more elegant specially if he writes a big description