0

I want to select the list item when I click the button(Which is inside the list box for every row).

Now I have try like this:-

   <ListBox Height="444"
                     ItemsSource="{Binding StudentDetails,Mode=TwoWay}" 
                     HorizontalAlignment="Left" Margin="2,34,0,0"
                     Name="listBox1" VerticalAlignment="Top" 
                     Width="476" BorderBrush="#00410D0D" 
                     SelectedIndex="{Binding MemberPrivacy,Mode=TwoWay}"
                     SelectedItem="{Binding SelectedStudent, Mode=TwoWay}">               
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Border BorderBrush="Gray" Padding="5" BorderThickness="1">
                            <StackPanel Orientation="Horizontal">
                                <Border BorderBrush="Wheat" BorderThickness="1">
                                    <Image  Name="ListPersonImage" Source="{Binding PersonImage}" Height="100" Width="100" Stretch="Uniform" Margin="10,0,0,0"/>
                                </Border>
                                <TextBlock Text="{Binding FirstName}" Name="firstName" Width="200" Foreground="White" Margin="10,10,0,0" FontWeight="SemiBold" FontSize="22"  />
                                <Image Height="50" Source="{Binding addImage}" HorizontalAlignment="Left"  Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="50" />

                                <Button Margin="-100,0,0,0" Height="80" Width="80" DataContext="{Binding DataContext, ElementName=listBox1}" Command="{Binding addPerson}" >
                                    <Button.Background>
                                        <ImageBrush ImageSource="{Binding addImage,  Converter={StaticResource pathToImageConverter}}" Stretch="Fill" />
                                    </Button.Background>
                                </Button>
                            </StackPanel>
                        </Border>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox> 

My View Model:-

public ListBoxEventsViewModel()
        {            
            addPerson = new ReactiveAsyncCommand();
            addPerson.Subscribe(x =>
            {
                MessageBox.Show("Test Button Selected..");
            });

        }

Here I can show the message box when I click the button. But I can not select the list item. Please let me know to solve this problem.

Thanks in advance.

View Model:- List box selected Item:-

public ListBoxEventsModel _SelectedStudent;
        public ListBoxEventsModel SelectedStudent
        {
            get { return _SelectedStudent; }
            set
            {
                this.RaiseAndSetIfChanged(x => x.SelectedStudent, value);
                MessageBox.Show("Selected index==>" + SelectedStudent.FirstName);
            }
        }

Here it showing the selected name when I click the list item. But this same thing I want to write it for the button(addImage)

Vijay
  • 3,152
  • 3
  • 24
  • 33
  • Another thing to note, in later versions of ReactiveUI ReactiveAsyncCommand was made depreciated and combined with ReactiveCommand. – Glenn Watson May 10 '14 at 23:28

1 Answers1

0

Bind the SelectedItem on the ListBox to a "SelectedItem" property on the ViewModel.

eg SelectedItem="{Binding SelectedItem, Mode=TwoWay}"

then on the view model just add a property of SelectedItem, then you can just set that from in your subscribe.

Glenn Watson
  • 2,758
  • 1
  • 20
  • 30
  • Hi @Glenn Watson. I follow your instruction. I can select the item when I click the list items. But I want use the button only. I want to write two functions for list box. One for list items. Another one for button. Both are different functions. Please help me to solve the problem. – Vijay May 12 '14 at 04:10
  • Hi @Glenn.. I have add the coding which is following by your instruction.. Please refer that and give me any idea to solve my problem. Here I am facing another problem. List box item is not responding on second click. – Vijay May 12 '14 at 04:23
  • Just have the binding refer to the SelectedItem binding, you don't need to have any code doing anything until the button is pressed. – Glenn Watson May 20 '14 at 00:08