【问题标题】:Return Value to the MainWindows [duplicate]返回值到 MainWindows [重复]
【发布时间】:2014-07-30 18:19:21
【问题描述】:

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

问题:
我试图找到一个解决方案,将行中的“_a”数据传输到 MainWIdow,但由于性能问题,它并没有那么顺利。我想让它更有效率。

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

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

【问题讨论】:

  • 虽然我接受“重复”问题不是完全重复,但它确实为您提供了一个很好的解决方案,可以使用delegates,有点像自定义事件,但用途更广泛。此外,它还通过另一个使用delegates 的示例链接到另一个问题。您可以使用delegates 将数据传递给任何类型的孩子和父母,包括从Window 代码后面到UserControl 代码后面,只要它们之间存在关系。
  • 抱歉,我的意思是链接到 How to call functions in a main view model from other view models? 问题,它向您展示了一个不同的示例并链接到我确实链接到的示例。

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


【解决方案1】:

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

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

最好的方法是使用 ViewModel。将其分配给 Test 并在其他表单中使用相同的 ViewModel 来获取选定的值。

阅读相关MSDN article

【讨论】:

    猜你喜欢
    • 2015-07-04
    • 2015-07-25
    • 2018-03-01
    • 2023-03-06
    • 1970-01-01
    • 2021-12-05
    • 1970-01-01
    • 2018-06-25
    • 1970-01-01
    相关资源
    最近更新 更多