1

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?

rlcrews
  • 3,482
  • 19
  • 66
  • 116
  • are you trying to get the value of a property of the SelectedAgency object in your view model? Where are you trying to get its value in your viewmodel, is it in the SelectionCHnaged method? – J King Jan 23 '14 at 21:28
  • yes I am trying to pick up the text binding value on the selected item at selection changed event. The property within my view model was an string with INPC setup to detect changes. The event fires correctly as I can see the INPC trying to set the string in the VM it is just not setting it to the correct value (object vs property within object) – rlcrews Jan 23 '14 at 21:31
  • I don't think you need to add SelectedValuePath to the list view. Are you trying to get the current or previous selection's Agency Name? – J King Jan 23 '14 at 21:33
  • can you show the declaration of your itemsource Agency list and your Selected item from the view model – J King Jan 23 '14 at 21:39
  • When the listbox is bound it shows two textboxes one showing a count and the other showing a name. All I am trying to do is when the item is clicked on set the property in my view model to the text name of the current selected item. This works fine with a single property but using an itemstemplate I cannot get to the property – rlcrews Jan 23 '14 at 21:55
  • sorry, a little more clarity. So do you want another control in the view to bind to the name of the selected item or is it the individual items in the list box that are not displaying correctly. (+1 for clearly documenting.) – J King Jan 23 '14 at 22:20
  • no I am not concerned about the view. I simply want to pass one value from the items template (which contains two bindings) to the view model. Example: The items template contains two text fields textA bound to propertyA and textB bound to propertyB from the collection return from a prior event, when the selected item is clicked within the current listbox I want to pass to the view model the value from propertyA and not the entire object. What I am missing is how to get the value of just one of the binding properties when a selection change occurs on an itemstemplate – rlcrews Jan 24 '14 at 01:12
  • possible duplicate: http://stackoverflow.com/questions/4902039/difference-between-selecteditem-selectedvalue-and-selectedvaluepath – Lee O. Jan 24 '14 at 01:33
  • I think you are missing the point of WPF binding. The object that represents the selected item of your listbox is already in your view model and is updated when you select a different item. You do not need to pass back the value of bound text fields from your item template as they are bound to your selected item and update as well. Just use " SelectedAgency.PropertyA " in your selection changed event to get the value. Do you want to capture the lost focus event of the text fields in the tiem template. Can you clarify why you want to do with this "textfield" value. – J King Jan 24 '14 at 01:47
  • 1
    Cant you clearly state the problem you are facing? – Mujahid Daud Khan Jan 24 '14 at 05:46

0 Answers0