1
votes

I'm kind of new to wpf templating and binding, and that's why have faced up with the following issue:

Let's assume that I have following objects in my application.

XAML:

<ComboBox Name="ComboModel" Width="230" Height="25" Grid.Row="3" Grid.Column="1"         HorizontalAlignment="Left" IsEnabled="True" >
        <ComboBox.ItemTemplate>
                  <DataTemplate>
                    <TextBlock>
                     <TextBlock.Text>
                      <MultiBinding StringFormat="{}{0}, {1}: {2}-{3}">
                       <Binding Path="ModelName"/>
                       <Binding Path="ModelBody"/>
                       <Binding Path="ModelFromYear"/>
                       <Binding Path="ModelToYear"/>
                      </MultiBinding>
                     </TextBlock.Text>
                    </TextBlock>
                   </DataTemplate>
         </ComboBox.ItemTemplate>
    </ComboBox>

CODE: Combobox is bound to List of ModelYearClass objects.

public class ModelYearClass
{
    private string modelName;
    public string ModelName
    {
        get { return modelName; }
        set
        {
            modelName = value;
        }
    }
    private string modelMake;
    public string ModelMake
    {
        get { return modelMake; }
        set
        {
            modelMake = value;
        }
    }
    private string modelBody;
    public string ModelBody
    {
        get { return modelBody; }
        set
        {
            modelBody = value;
        }
    }
    private string modelYear;
    public string ModelYear
    {
        get { return modelYear; }
        set
        {
            modelYear = value;
        }
    }
    private int modelId;
    public int ModelId
    {
        get { return modelId; }
        set
        {
            modelId = value;
        }
    }
    private string modelFromYear;
    public string ModelFromYear
    {
        get { return modelFromYear; }
        set
        {
            modelFromYear = value;
        }
    }
    private string modelToYear;
    public string ModelToYear
    {
        get { return modelToYear; }
        set
        {
            modelToYear = value;
        }
    }
}
    ...
    List<ModelYearClass> ModelYearList;
    ...
    ComboModel.ItemsSource = ModelYearList;
    ComboModel.SelectedValuePath = "@ModelId";
    ComboModel.SelectedValue = 2;

Everything is fine - the combobox is populated with what I want, but I'm not able to assign a value I need, by these lines.

ComboModel.SelectedValuePath = "@ModelId";
ComboModel.SelectedValue = 2;

I think maybe it's done somehow else for a combobox with multibindig?

How to achieve this? Please help me.

1

1 Answers

1
votes

Simply removing the @ from "@ModelId" works for me.

ComboModel.SelectedValuePath = "ModelId";

Usually the SelectedValuePath property of a ComboBox never changes. If so, consider setting it in XAML.

<ComboBox SelectedValuePath="ModelId" ... >
...
</ComboBox>