开发者

Problem showing selected value of combobox when it is bind to a List<T> using Linq to Entities

I have a ComboBox which is has an ItemTemplate applied on it and is bind to a List of entity return using linq. I'm using mvvm. It is bind to it successfully but when I set the selected value of it from code at runtime to show the selected value coming from db it doesn't select it. For reference here is my ComboBox xaml.

<DataTemplate x:Key="ManufacturerDataTemplate">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="50"/>     
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>    
        <Image x:Name="imgManufacturer" Width="25" Height="25"
               Source="{Binding Path=ManufacturerImage}" Grid.Column="0"/>
        <TextBlock x:Name="txtManufacturer" Grid.Column="1" HorizontalAlignment="Left"
                   VerticalAlignment="Center" Text="{Binding Path=ManufacturerName}"
                   Tag="{Binding Path=ManufacturerID}"/>
    </Grid>
</DataTemplate>


<ComboBox x:Name="cboManufacturer" 
          SelectionChanged="cboManufacturer_SelectionChanged"
          ItemsSource = "{Binding Path=CurrentManufacturers}"                        
          SelectedValue="{Binding Path=SelectedManufacturer}"                        
          Grid.Column="3" Grid.Row="2"  Margin="20,9.25,68,7.75"  
          ItemTemplate="{StaticResource ManufacturerDataTemplate}" TabIndex="6"/>

Here is my part from code behind from viewModel.

List<tblManufacturer> currentManufacturers
        = new List<tblManufacturer>();

tblManufacturer selectedManufacturer = null;

public List<tblManufacturer> CurrentManufacturers
{
    get
    {
 开发者_C百科       return currentManufacturers;
    }
    set
    {
        currentManufacturers = value;
        NotifyPropertyChanged("CurrentManufacturers");
    }
}

public tblManufacturer SelectedManufacturer
{
    get
    {
        return selectedManufacturer;
    }
    set
    {
        selectedManufacturer = currentManufacturers.Where(mm => mm.ManufacturerID == Convert.ToInt32(selectedDevice.tblManufacturer.EntityKey.EntityKeyValues[0].Value)).First();
        NotifyPropertyChanged("SelectedManufacturer");
    }
}


Here is the sample code snippet:

Xaml for ComboBox:

 <ComboBox ItemsSource="{Binding ManufacturerList}" DisplayMemberPath="Name" SelectedValuePath="ID"
              SelectedItem="{Binding SelectedManufacturer}"/>

ViewModel code :

 public class Manufacturer
{
    public int ID { get; set; }

    public string Name { get; set; }
}





 private List<Manufacturer> _manufactuerlist;
   private Manufacturer _selectedManufacturer;

 private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

#region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    public Manufacturer  SelectedManufacturer
    {
        get
        {
          return _selectedManufacturer;
        }
        set
        {
            _selectedManufacturer = value;
            NotifyPropertyChanged("SelectedManufacturer");
        }
    }

    public List<Manufacturer> ManufacturerList
    {
        get
        {
            return _manufactuerlist;
        }
        set
        {
            _manufactuerlist = value;
            NotifyPropertyChanged("ManufacturerList");
        }
    }

And finally Set the Selected Manufacturer in your view model like this:

SelectedManufacturer = _manufactuerlist.Find(m => m.ID == 2);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜