This is a migrated thread and some comments may be shown as answers.

SelectedIem databinding

1 Answer 54 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Todd Hile-Hoffer
Top achievements
Rank 2
Todd Hile-Hoffer asked on 06 Jan 2011, 11:00 PM
For exaple binding a text box to an item on my Model is easy. How do I bind the Combobox to an item on my Model?


I am able to bind the list of items, but I can't figure out how to get the selected item to be the selected item in my viewmodel class. Please help. I am learning Silverlight to use for new application and I am totally stuck on this... Below is the xaml then code behind then model. Even just an example of how to use the SelectedItem of a Combobox as the Object of the Parent Item would be great.
xaml
<StackPanel Orientation="Vertical"    Name="spContainer" Margin="10 30 500 0">
         <TextBlock Name="tbId"  Text="{Binding ObjectFoo.Id, Mode=TwoWay}" Height="30">
               
         </TextBlock>
         <telerik:RadComboBox  x:Name="rcbAssetTeams"   ItemsSource="{Binding AssetTeams}" SelectedValue="{Binding ObjectFoo.AssetTeam, Mode=TwoWay}"  telerik:StyleManager.Theme="Office_Blue"  Margin="0,0,0,0"  >
             <telerik:RadComboBox.ItemTemplate >
                 <DataTemplate x:Name="AssetTeamTemplate" >
                     <StackPanel Orientation="Horizontal">
                         <TextBlock Text="{Binding AssetTeamDesc}" />
                     </StackPanel>
                 </DataTemplate>
             </telerik:RadComboBox.ItemTemplate>
         </telerik:RadComboBox>
     </StackPanel>

codebehind
public partial class ObjectFooPage : Page
   {
       private int ObjectFooId;
       private ViewModel.ObjectFooViewModel ViewModel;
       public ObjectFooPage()
       {
           InitializeComponent();
             
       }
       // Executes when the user navigates to this page.
       protected override void OnNavigatedTo(NavigationEventArgs e)
       {
           ObjectFooId = int.Parse(this.NavigationContext.QueryString["Id"]);
           ViewModel = new ViewModel.ObjectFooViewModel(ObjectFooId);
           this.spContainer.DataContext = ViewModel;
             
       }
       
        
          
   }


ViewModel
public class ObjectFooViewModel : Telerik.Windows.Controls.ViewModelBase 
   {
       LeDataService.Service1Client _client;
       private ObservableCollection<Model.AssetTeamContainer> _AssetTeams;
       private Model.ObjectFooContainer _OjbectFoo; 
       private void EnsureClient()
       {
           if (_client == null)
               _client = new LeDataService.Service1Client();
       }
       public ObservableCollection<Model.AssetTeamContainer> AssetTeams
       {
           get
           {
               return _AssetTeams;
           }
           set
           {
               if (_AssetTeams != value)
               {
                   _AssetTeams = value;
                   OnPropertyChanged("AssetTeams");
               }
           }
       }
       public Model.ObjectFooContainer ObjectFoo
       {
           get { return _OjbectFoo; }
           set
           {
               if (_OjbectFoo != value)
               {
                   _OjbectFoo = value;
                   OnPropertyChanged("ObjectFoo"); 
               }
           }
       }
       public ObjectFooViewModel(int Id)
       {
           EnsureClient();
           _client.GetAssetTeamsCompleted += new EventHandler<LeDataService.GetAssetTeamsCompletedEventArgs>(_client_GetAssetTeamsCompleted);
           _client.GetAssetTeamsAsync();
           _client.GetFooCompleted += new EventHandler<LeDataService.GetFooCompletedEventArgs>(_client_GetFooCompleted);
           _client.GetFooAsync(Id);
       }
       void _client_GetFooCompleted(object sender, LeDataService.GetFooCompletedEventArgs e)
       {
           if (e.Error != null)
           {
               throw e.Error;
           }
           ObjectFoo = new Model.ObjectFooContainer(e.Result); 
             
       }
       void _client_GetAssetTeamsCompleted(object sender, LeDataService.GetAssetTeamsCompletedEventArgs e)
       {
           if (e.Error != null)
           {
               throw e.Error;
           }
           if (_AssetTeams == null)
               _AssetTeams = new ObservableCollection<Model.AssetTeamContainer>();
           _AssetTeams.Clear();
           var result = new ObservableCollection<Model.AssetTeamContainer>();
           foreach (var item in e.Result)
           {
               result.Add(new Model.AssetTeamContainer(item));
           }
           AssetTeams = result; 
       }
   }

1 Answer, 1 is accepted

Sort by
0
Accepted
Sergi
Top achievements
Rank 1
answered on 11 Jan 2011, 03:30 PM
Instead of trying to bind to the SelectedItem property, I'd have an IsSelected property in my viewmodels (in your case AssetTeamContainer, it should be a ViewModel). Then, you place a bind between the IsSelected property of the RadComboBoxItems and you own items.

In WPF that would be easy, just setting it in the ItemContainerStyle, but in Silverlight you can't set a binding in a setter, so you'd need to follow any of these two approaches:

http://bea.stollnitz.com/blog/?p=55 (explained near the end)

http://blogs.msdn.com/b/delay/archive/2009/11/02/as-the-platform-evolves-so-do-the-workarounds-better-settervaluebindinghelper-makes-silverlight-setters-better-er.aspx

Afterwards you'd just work with your own IsSelected properties.

Regards,
-Sergi
Tags
ComboBox
Asked by
Todd Hile-Hoffer
Top achievements
Rank 2
Answers by
Sergi
Top achievements
Rank 1
Share this question
or