返回MainWindows的值

时间:2014-07-30 10:12:42

标签: c# wpf xaml visual-studio-2013

目标:
单击lstView_bbb中的一行(在Test.xaml中)时,返回值应返回到MainWindow类(MainWindow.xaml)。然后,Test.xaml将被关闭。

问题:
我试图找到一个解决方案,来自行的“_a”数据应该转移到MainWIndow,但由于性能问题,它没有那么顺利。我想让它更有效率。

信息:
*我在VS 2013中使用WPF * MainWIndow.xaml类的返回值为“_a”

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;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

最简单的方式是在Test中创建一个属性:

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

最佳方式是使用ViewModel。将其分配给Test并使用其他形式的相同ViewModel来获取所选值。

阅读相关的MSDN article