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
codebehind
ViewModel
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;
}
}