0

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}&#x0a;{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. The dropdown list of the combobox

But when I select one item I get this: The item selected showing the Object.tostring()

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;: if i comment 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.

After select one item

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

  • I don't see anything that would mess up the `DataTemplate` or the selected item, unless there's something in the `comboBoxCampaign` `Style`? – Keith Stein Jun 01 '20 at 02:04
  • There is a `public ObservableCollection Nombres` property, so `ItemsSource="{Binding Path=Nombres}"` will obviously work, provided that the DataContext of the view is set to an instance of the class that owns the Nombres property, i.e. `DataContext = this;`. However, `SelectedValuePath="Value"` and `SelectedValue="{Binding Path=NombreCampana}"` make no sense, because there is neither a `Value` property in class Campana, nor a `NombreCampana` property in class menuPrincipal. It is also totally unclear why you bind the Text property of the ComboBox. – Clemens Jun 01 '20 at 07:16
  • I have edit the post and answer your question Clemens, and i show the style, deleting one property of the style helps but is not exactly what i want but at least is an improve – David Pinedo Solano Jun 01 '20 at 10:57

1 Answers1

0

Finally i get the answer in this post Can I use a different Template for the selected item in a WPF ComboBox than for the items in the dropdown part?

Simple swapping between two templates if the item is selected or not.