从WPF MVVM应用程序将参数传递给WPF用户控件库

时间:2013-10-10 14:18:27

标签: c# wpf xaml mvvm

Greatings,我正在创建一个wpf用户库控件,它有一个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 App,带有参考库的MainPage(XAML)中:

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

WPF App,MainPage(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 
        }

但这不起作用。

3 个答案:

答案 0 :(得分:0)

使用EventHandler Delegate发布和订阅活动。信息准备好后,提出事件并传递EventArgs

中所需的信息

答案 1 :(得分:0)

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

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(相反,但仍为您提供有效的方法)

更新&gt;&gt;&gt;

我真的不明白你的问题。如果您真的不想遵循我给您的链接中的任何建议,只需在内部创建一个承载UserControl控件的Windows窗体ReportViewer并声明您需要的所有属性{{ 1}}。然后像这样使用XAML:

UserControl

答案 2 :(得分:0)

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

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