【问题标题】:WPF Pass object from window's Usercontrol to another window's UsercontrolWPF 将对象从窗口的用户控件传递到另一个窗口的用户控件
【发布时间】:2021-11-12 17:07:52
【问题描述】:

我有 2 个窗口和 2 个用户控件第一个窗口使用第一个用户控件在列表视图中显示有关对象的简要信息,现在当用户单击列表中的对象(行)时,第二个窗口应该打开第二个用户控件显示该对象的完整信息。

所以问题是我如何将该对象从 UC1 传递到 UC2。

【问题讨论】:

标签: c# wpf wpf-controls


【解决方案1】:

您的问题的答案是使用 MVVM。例如:

列表窗口:

<Window>
    <ItemsControl ItemSource = {Binding DataItems}>
        <ItemsControl.ItemTemplate>
            <userControls:YourLessDetailedUserControl/>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Window>

详细信息窗口:

<Window>
         <userControls:YourMoreDetailedUserControl DataContext = {Binding DataItem}/>
</Window>

视图模型:

class MainWindowViewModel
{
     public List<DataItemViewModel> DataItems {get;}
}

class DataItemViewModel
{
     public ICommand OpenInDetailedWindow {get;}

     //more properties here to describe your data item
}

class DetailedWindowViewModel
{
    DataItemViewModel DataItem {get;}
}

DataItemViewModel 中的ICommand 应定义为打开详细信息窗口,其中DetailedWindowViewModel.DataItem 设置为DataItemViewModel

如果您是 MVVM 新手,这一切看起来都非常陌生。不过没关系!一开始每个人都很难。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多