C# 从WPF MVVM应用程序向WPF用户控件库传递参数
Greatings,我正在创建一个wpf用户库控件,它有一个windows窗体控件。是否可以将值传递给属性类库控件(而不是windows窗体控件属性)?,我有: WPF用户控制库(XAML):C# 从WPF MVVM应用程序向WPF用户控件库传递参数,c#,wpf,xaml,mvvm,C#,Wpf,Xaml,Mvvm,Greatings,我正在创建一个wpf用户库控件,它有一个windows窗体控件。是否可以将值传递给属性类库控件(而不是windows窗体控件属性)?,我有: WPF用户控制库(XAML): public partial class ReportViewer : UserControl { public static readonly DependencyProperty UrlReportServerProperty = DependencyProp
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();
}
<WPFControlsSol:ReportViewer HorizontalAlignment="Left" Margin="0,0,0,0"
VerticalAlignment="Top" Width="644"
UrlReportServer="{Binding Url}"
</WPFControlsSol:ReportViewer>
public partial class MainPageView : Window
{
public MainPageView()
{
InitializeComponent();
ViewModel viewModel = new ViewModel();
DataContext = 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应用程序中,主页(XAML)和参考库:
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();
}
<WPFControlsSol:ReportViewer HorizontalAlignment="Left" Margin="0,0,0,0"
VerticalAlignment="Top" Width="644"
UrlReportServer="{Binding Url}"
</WPFControlsSol:ReportViewer>
public partial class MainPageView : Window
{
public MainPageView()
{
InitializeComponent();
ViewModel viewModel = new ViewModel();
DataContext = 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
}
在ViewModel中:
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();
}
<WPFControlsSol:ReportViewer HorizontalAlignment="Left" Margin="0,0,0,0"
VerticalAlignment="Top" Width="644"
UrlReportServer="{Binding Url}"
</WPFControlsSol:ReportViewer>
public partial class MainPageView : Window
{
public MainPageView()
{
InitializeComponent();
ViewModel viewModel = new ViewModel();
DataContext = 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
}
但这不起作用 使用发布和订阅事件。信息准备就绪后,发起活动并传递
EventArgs
搜索internet时所需的信息,我为您找到了许多解决方案。请看以下几页:
MSDN上的第页显示如何将WPF控件属性转换为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>
您正在谈论的是。卡特尔为您提供现成的解决方案。以它为例,或者将其用作应用程序的框架,这取决于您
另一个很好的功能是您可以。请确切地告诉我们,通过传递这些参数,您想要实现什么,以及您正在谈论哪些参数。我有一个WPF MVVM应用程序,需要加载报表(.rdl),报表托管在服务器报表中,我需要使用凭据访问报表(urlserver、报告路径、用户、密码)并传递报表中显示的参数。唯一可以执行此操作的控件是来自Windows窗体的ReportViewer,我想创建一个具有ReportViewer控件的WPF控件用户库。我的想法是在其他WPF应用程序中使用此WPF控件,其他报表仅加载控件并传递凭据和参数。为什么要使用WindowsFormsHost cont在WPF控件库中托管WPF用户控件的rol?可能是WindowsFormsHost应该从WPF用户控件的C代码中创建和加载…!!是的,我正在使用WindowsFormsHost…我要测试在WPF用户控件的C中创建和加载的ReportViewer控件。如何将ReportViewer控件添加到C中的WindowsFormsHost?我的情况是,参数不是reportviewer的属性,参数是访问报表的变量,我不知道如何将此类参数传递给WPF用户控件库。我尝试了添加控件属性,正如您所建议的,但不起作用。我有一个WPF MVVM应用程序需要加载报表(.rdl),报表托管在服务器报表中,我需要使用凭据(urlserver、报表路径、用户、密码)访问报表并为“在报表中显示”传递参数。唯一可以做到这一点的控件是来自Windows窗体的ReportViewer,我想创建一个具有ReportViewer控件的WPF控件用户库。我的想法是在其他WPF应用程序中使用此WPF控件,其他报表仅加载控件并传递凭据和参数。伙计,重复自己的想法没有帮助。不管怎样,我现在已经做完了,祝你好运。