I have a wpf listbox with an items template
<ListBox Margin="2" Background="{x:Null}" BorderBrush="{x:Null}" HorizontalContentAlignment="Stretch" ItemsSource="{Binding AgencyItems}" SelectedItem="{Binding Path=SelectedAgency, Mode=TwoWay}" SelectedValuePath="AgencyName" ItemTemplate="{DynamicResource agencyItemsTemplate}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<ei:CallMethodAction
TargetObject="{Binding}"
MethodName="GetSubmittedItems"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ListBox>
data template
<DataTemplate x:Key="agencyItemsTemplate">
<Grid HorizontalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition Height="0.62*"/>
<RowDefinition Height="0.38*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Rectangle Fill="#FF4485B6" Margin="1" Grid.RowSpan="2" Stroke="#FFA1A1A1" RadiusX="3" RadiusY="3" StrokeThickness="0.6" />
<TextBlock Margin="0,2,2,2" TextWrapping="Wrap" Text="{Binding Path=SubmittedItemsCount, StringFormat='Total Items: {0}'}" Grid.Row="1" FontSize="13.333" TextAlignment="Right"/>
<TextBlock Margin="5,2,2,2" TextWrapping="Wrap" Text="{Binding Path=AgencyName}" VerticalAlignment="Stretch" FontWeight="Bold" FontSize="14.667" Grid.RowSpan="1"/>
</Grid>
</DataTemplate>
The collection that is bound to the listbox contains two properties which you can see bound in the data template
public class SubmitingAgencyItem
{
public string AgencyName { get; set; }
public int SubmittedItemsCount { get; set; }
}
Within the ViewModel I have my property set with inotify (note I am using the SimpleMVVM framework so INPC events are handled through this lamda expression)
private string selectedAgency;
public string SelectedAgency
{
get { return selectedAgency; }
set
{
selectedAgency = value;
NotifyPropertyChanged(m => m.SelectedAgency);
}
}
The selectionChanged event is routed to the VM using event triggers/blend behaviors
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<ei:CallMethodAction
TargetObject="{Binding}"
MethodName="GetContributorAgencies"/>
</i:EventTrigger>
The items source is being set from the methods wihtin the Viewmodel
public void GetContributorAgencies()
{
SubmissionPurgeModel pm = new SubmissionPurgeModel();
if (SelectedContributor != null)
{
AgencyItems = pm.GetContributorData(SelectedContributor);
}
}
Agency Items is an observableCollection of SubmittingAgencyItem
class
When using a complex type how do you pass one property of the selected item to the viewmodel? I've tried SelectedValue
and SelectedValuePath
but it only seems to pass objects namespace back to the viewmodel?