SelectedIem databinding

2 posts, 1 answers
  1. Todd Hile-Hoffer
    Todd Hile-Hoffer avatar
    19 posts
    Member since:
    Dec 2008

    Posted 06 Jan 2011 Link to this post

    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; 
           }
       }
  2. Answer
    Sergi
    Sergi avatar
    4 posts
    Member since:
    Dec 2010

    Posted 11 Jan 2011 Link to this post

    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
  3. DevCraft banner
Back to Top