c# - 从WPF MVVM应用程序将参数传递给WPF用户控制库

标签 c# wpf xaml mvvm

好的,我正在创建一个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 
        }

但这行不通。

最佳答案

使用EventHandler Delegate发布和订阅事件。信息准备就绪后,引发事件并传递EventArgs中要求的信息

关于c# - 从WPF MVVM应用程序将参数传递给WPF用户控制库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19298341/

相关文章:

wpf - 实时移动矩形时半透明窗口在多个屏幕上的性能

c# - 如何使自定义 MarkupExtension 从 XAML 文件可见/可访问?

c# - winform导入excel表

C# 格式化类方法的输出,现在不显示任何数据

c# - 访问 C#/WPF 中 IMultiValueConverter.ConvertBack() 中的绑定(bind)数据

wpf - 如何防止 VS WPF 设计器为工具箱中的每个对象添加边距

c# - 与 xaml View 模型交互

xaml - 如何正确地将功能区库类别绑定(bind)到集合

c# - 将一维数组的索引转换为二维数组 i。 e.行和列

c# - 如何查看由 xml-comments 生成的完整文档