【问题标题】:Pass parameters to WPF User Control Library from WPF MVVM app从 WPF MVVM 应用程序将参数传递给 WPF 用户控件库
【发布时间】:2013-10-18 08:45:47
【问题描述】:

Greatings,我正在创建一个 wpf 用户库控件,它有一个 Windows 窗体控件。是否可以将值传递给属性类库控件(不是 windows 窗体控件属性)?,我有这个:

WPF 用户控件库 (XAML):

<wfi:WindowsFormsHost Height="300" Name="winFormsHost" VerticalAlignment="Top" >

   <wfr:ReportViewer x:Name="rptViewer" ProcessingMode="Remote"/>

</wfi:WindowsFormsHost>

....

WPF 用户控件库 (C#):

public partial class ReportViewer : UserControl
{

        public static readonly DependencyProperty UrlReportServerProperty =
            DependencyProperty.Register("UrlReportServer", typeof(string),    typeof(ReportViewer),
                                        new PropertyMetadata((string)""));
.... // Other Dependecies properties

     public string UrlReportServer
     {
        get { return (string)GetValue(UrlReportServerProperty);}
        set { SetValue(UrlReportServerProperty, value); }
     }
............ // Other properties

     public ReportViewer()
     {
            InitializeComponent();
            this.DataContext = this;

            ReportViewerLoad();
     }
     public void ReportViewerLoad()
     {
             rptViewer.ProcessingMode = ProcessingMode.Remote;

             rptViewer.ServerReport.ReportServerUrl =
                        new Uri(UrlReportServer);
...... //Pass credentials to server reports and parameters to Report with Properties.

             rptViewer.ServerReport.Refresh();
                this.rptViewer.RefreshReport();
     } 

在 WPF 应用程序中,带有参考库的 MainPage (XAML):

<WPFControlsSol:ReportViewer HorizontalAlignment="Left" Margin="0,0,0,0" 
                             VerticalAlignment="Top" Width="644"
                             UrlReportServer="{Binding Url}"
</WPFControlsSol:ReportViewer>

WPF 应用,主页 (C#):

public partial class MainPageView : Window
{
        public MainPageView()
        {
            InitializeComponent();

            ViewModel viewModel = new ViewModel(); 
            DataContext = viewModel;

        }
}

在 ViewModel 中:

public class ViewModel : ViewModelBase
{

        private string _url;  .... // Other attributes

        public string Url
        {
            get { return _url; }
            set 
            {
                if (_url != value)
                {
                    _url = value;
                    RaisePropertyChanged("Url"); //Notification Method own MVVM  Template I use.
                }
            }
        } .... // Other properties 



        public ViewModel()
        {

           LoadReport();
        }  



        public void LoadReport()
        {
            Url = "http://IPSERVER"; .... // Other properties 
        }

但这不起作用。

【问题讨论】:

  • 请准确告诉我们您通过传递这些参数想要实现的目标以及您所谈论的参数。
  • 我有一个 WPF MVVM 应用程序需要加载报告 (.rdl),报告托管在服务器报告中,我需要使用凭据(urlserver、报告路径、用户、密码)访问报告和传递参数以在报告中显示。唯一可以做到这一点的控件是 Windows 窗体中的 ReportViewer,我想做一个具有 ReportViewer 控件的 WPF 控件用户库。我的想法是在其他 WPF App 中使用这个 WPF 控件和其他报表只加载控件并传递凭据和参数。
  • 为什么在 WPF 控件库中使用 WindowsFormsHost 控件来托管 WPF 用户控件?可能应该从您的 WPF 用户控件的 C# 代码中创建和加载 WindowsFormsHost...!!
  • 是的,我正在使用 WindowsFormsHost... 我去测试在 WPF 用户控件的 C# 中创建和加载的 ReportViewer 控件。
  • C#中如何将ReportViewer控件添加到WindowsFormsHost?

标签: c# wpf xaml mvvm


【解决方案1】:

使用EventHandler Delegate 发布和订阅事件。当信息准备好时,提出事件并传递EventArgs中所需的信息

【讨论】:

    【解决方案2】:

    您说的是nested user controls problem。 Catel 为您提供开箱即用的解决方案。看看它作为一个例子,或者只是将它用作你的应用程序的框架,这取决于你。

    另一个很棒的功能是你可以map properties between views and view models via easy attributes

    【讨论】:

      【解决方案3】:

      在互联网上搜索,我为您找到了许多解决方案。请查看以下页面:

      Walkthrough: Using ReportViewer in a WPF Application

      Using Report Viewer Control in WPF

      Using a Report Viewer Control in Windows Presentation Foundation (WPF)

      Using MS ReportViewer in WPF contains a good tip

      MSDN 上的WindowsFormsHost.PropertyMap Property 页面显示了如何将 WPF 控件属性转换为 WinForms 属性,反之亦然。

      Pass parameters from WPF user control to Windows Form User Control via WindowsFormsHost

      Integrate WPF UserControls in WinForms(反之亦然,但仍然为您提供有效的方法)

      更新>>>

      我真的不明白你的问题。如果你真的不想听从我给你的那些链接的任何建议,只需创建一个 Windows 窗体 UserControl,它在内部托管 ReportViewer 控件,并在 UserControl 上声明你需要的所有属性。然后像这样使用 XAML:

      <wfi:WindowsFormsHost Height="300" Name="winFormsHost" VerticalAlignment="Top" >
          <YourWinFormsUserControlWithInternalReportViewer UrlServer="Something" 
              Path="Some/Path/Report.rdl" User="Geert" Password="password" />
      </wfi:WindowsFormsHost>
      

      【讨论】:

      • 我的情况是参数不是reportviewer的属性,参数是访问report的变量,不知道如何将这类参数传递给WPF用户控件库。
      • 我尝试向 Control 添加属性,就像您建议的那样,但不起作用。我有一个需要加载报告 (.rdl) 的 WPF MVVM 应用程序,报告托管在服务器报告中,我需要使用凭据(urlserver、报告路径、用户、密码)访问报告并传递参数以在报告中显示。唯一可以做到这一点的控件是 Windows 窗体中的 ReportViewer,我想做一个具有 ReportViewer 控件的 WPF 控件用户库。我的想法是在其他 WPF App 中使用这个 WPF 控件和其他报表只加载控件并传递凭据和参数。
      • 老兄,重复自己没有帮助。不管怎样,我现在已经完成了,祝你好运。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-10
      • 1970-01-01
      • 1970-01-01
      • 2019-01-13
      • 2016-06-28
      • 1970-01-01
      • 2012-03-21
      相关资源
      最近更新 更多