Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
Greatings, I'm creating a wpf user library control, which has a windows form control. Is possible pass values to properties class library control (not windows forms control properties), I have this:

WPF User Control Library (XAML):


HTML
<UserControl x:Class="WPFControlsSol.ReportViewer"....

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

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

</wfi:WindowsFormsHost>


WPF User Control Library (C#):

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();
}


In WPF App, MainPage (XAML) with the reference library:

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

WPF App, MainPage (C#):
C#
public partial class MainPageView : Window
    {
        public MainPageView()
        {
            InitializeComponent();

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

        }
    }


In ViewModel:

C#
 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
}



But This not works.
Posted
Updated 9-Oct-13 12:40pm
v2
Comments
Sergey Alexandrovich Kryukov 9-Oct-13 18:53pm    
"Not works" is not informative.
—SA

1 solution

On your UrlReportServerProperty dependency property you need to provide a callback for when the property changes.

C#
public static readonly DependencyProperty.Register("UrlReportServer", typeof(string), typeof(ReportViewer), new PropertyMetadata("", PropertyChangedCallback));

        private static void PropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
        {
             var ctrl = dependencyObject as ReportViewer;
            if (ctrl != null)
            {
                if(dependencyPropertyChangedEventArgs.NewValue != null)
                    ctrl.ReportViewerLoad(dependencyPropertyChangedEventArgs.NewValue.ToString());
            }
        }


        public void ReportViewerLoad(string url)
        {
            rptViewer.ProcessingMode = ProcessingMode.Remote;
            rptViewer.ServerReport.ReportServerUrl = new Uri(url);
            //Pass credentials to server reports and parameters to Report with Properties.
            rptViewer.ServerReport.Refresh();
            this.rptViewer.RefreshReport();
        }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900