0

Goal:
When you click one of the rows in the lstView_bbb (in Test.xaml), the return value shall be return to the class MainWindow (MainWindow.xaml). And then, the Test.xaml shall be closed.

Problem:
I tried finding a solution that data of "_a" from row shall be transferred to the MainWIndow but it didn't go so well due to performance issue. I want to make it more efficient.

Information:
*I'm using WPF with VS 2013
*The return value is "_a" to the class MainWIndow.xaml

enter image description here

enter image description here

enter image description here

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button x:Name="btn_test" Content="Test" HorizontalAlignment="Left" Margin="348,240,0,0" VerticalAlignment="Top" Width="75" Click="btn_test_Click"/>

    </Grid>
</Window>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }


        private List<aaa> _myList_aaa;

        private void btn_test_Click(object sender, RoutedEventArgs e)
        {
            _myList_aaa = new List<aaa>();

            for (int a = 1; a <= 5; a++)
            {
                aaa myaaa = new aaa();

                myaaa._a = a;
                myaaa._city = "New Yor";
                myaaa._name = "jj jj jj";

                _myList_aaa.Add(myaaa);
            }

            Test myTest = new Test(_myList_aaa);

            myTest.ShowDialog();
        }

    }


    public class aaa
    {
        public int _a { get; set; }
        public string _name { get; set; }
        public string _city { get; set; }
    }

}









---------------------------------------


<Window x:Class="WpfApplication1.Test"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Test" Height="300" Width="300">
    <Grid Background="#FFE5E5E5">
        <ListView x:Name="lstView_bbb" SelectionMode="Single"  ItemsSource="{Binding}" HorizontalAlignment="Left" Height="111" Margin="35,67,0,0" VerticalAlignment="Top" Width="222">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="name" Width="auto" DisplayMemberBinding="{Binding Path=_name}" TextBlock.TextAlignment="Left"  />
                    <GridViewColumn Header="city" Width="auto" DisplayMemberBinding="{Binding Path=_city}" TextBlock.TextAlignment="Center"  />
                </GridView>
            </ListView.View>
        </ListView>

    </Grid>
</Window>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for Test.xaml
    /// </summary>
    public partial class Test : Window
    {
        public Test(IList<aaa> pAAA)
        {
            InitializeComponent();

            lstView_bbb.DataContext = pAAA;
        }
    }
}
HelloWorld1
  • 13,688
  • 28
  • 82
  • 145
  • While I accept that the 'duplicate' question is *not* an exact duplicate, it *does* provide a good solution for you to pass your data using `delegate`s, which are kind of like custom events, but far more versatile. Furthermore, it also links to another question with another example of using `delegate`s. You can use `delegate`s to pass data to and from children and parents of any kind and that includes from `Window` code behind to `UserControl` code behind as long as there is a relationship between them. – Sheridan Jul 30 '14 at 10:19
  • Sorry, I meant to link to the [How to call functions in a main view model from other view models?](http://stackoverflow.com/questions/19522202/how-to-call-functions-in-a-main-view-model-from-other-view-models) question instead, which shows you a different example and links to the one that I *did* link to. – Sheridan Jul 30 '14 at 10:28

1 Answers1

0

The easiest way is to create a property in Test:

class Test
{
    public aaa SelectedItem
    {
        get
        {
            return lstView_bbb.SelectedItems[0] as aaa;
        }
}

The best way is to use a ViewModel. Assign it to Test and use the same ViewModel in you other form to get the selected value.

Read the related MSDN article.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325