Home > DeveloperSection > Forums > Resetting binding's source in wpf
john rob

Total Post:108

Points:756
Posted on    August-19-2014 3:19 AM

 WPF WPF 
Ratings:


 1 Reply(s)
 1156  View(s)
Rate this:

I have this list box:

<ListBox DockPanel.Dock="Left" HorizontalAlignment="Left" Width="150"

                 ItemsSource="{Binding PcConfigurations, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}">

            <ListBox.ItemTemplate>

                <DataTemplate>

                    <TextBlock Text="{Binding}">

                        <i:Interaction.Triggers>

                            <i:EventTrigger EventName="MouseDoubleClick">

                                <cmd:EventToCommand Command="{Binding LocalConfigurationCommand}"

                                    CommandParameter="{Binding}"/>

                            </i:EventTrigger>

                        </i:Interaction.Triggers>

                    </TextBlock>

                </DataTemplate>

            </ListBox.ItemTemplate>

        </ListBox>

When I run the application I got this error in the output:

System.Windows.Data Error: 40 : BindingExpression path error: 'LocalConfigurationCommand' property not found on 'object' ''PcConfiguration_0CC914504C64AE357F440BEA28C5F73FD3627331B5E407B6D7DD75076453D393' (HashCode=20396001)'. BindingExpression:Path=LocalConfigurationCommand; DataItem='PcConfiguration_0CC914504C64AE357F440BEA28C5F73FD3627331B5E407B6D7DD75076453D393' (HashCode=20396001); target element is 'EventToCommand' (HashCode=27147755); target property is 'Command' (type 'ICommand')

I guess it is because the current path that the

EventToCommand Command="{Binding LocalConfigurationCommand}"....

is looking for is set to the 'location' of what the listbox has set in its binding.

So I'd like to ask how can I put there location of my ViewModel, so it can find that command, but the location in CommandParameter={Binding} will still be current item from ListBox.



Pravesh Singh

Total Post:411

Points:2881
Posted on    August-19-2014 3:37 AM

The viewmodel is in fact the ListBox's data context, and you can get to the ListBox by using RelativeSource:

<cmd:EventToCommand Command="{Binding Path=DataContext.LocalConfigurationCommand, RelativeSource={RelativeSource AncestorType=ListBox}}"

                    CommandParameter="{Binding}"/>


Don't want to miss updates? Please click the below button!

Follow MindStick