Passing constructor parameters in xaml file

Michell 45 Reputation points
2023-08-03T01:50:35.8633333+00:00

I would like to know how to pass constructor parameters in a xaml file?

Example:

NewChart.cs

public partial class NewChart : UserControl
{
    public MainWindowViewModel mainWindowViewModel { get; set; }
    public NewChart(MainWindowViewModel _instance)
    {
        InitializeComponent();
        mainWindowViewModel = _instance;

    }
}

In the XAML file, how can I pass the argument {Binding _instance} inside views:NewChart

I tried that but it didn't work

<views:NewChart MainWindowViewModel="{Binding _instance}">

Developer technologies .NET Other
Developer technologies XAML
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Hui Liu-MSFT 48,676 Reputation points Microsoft External Staff
    2023-08-03T02:34:12.1333333+00:00

    Hi,@Michell .Welcome Microsoft Q&A.

    In XAML, you could not directly pass constructor parameters when creating an instance of a UserControl. Constructor parameters are typically used to initialize the object and are not directly accessible from XAML.

    Instead, you can use Data Binding to pass data to the UserControl from its parent view (e.g., MainWindow).

    Here's how you could do it using Data Binding:

    NewChart.xaml:

     <Grid>
            <TextBlock Text="{Binding ChartName}" Width="200" Height="50" FontSize="20" FontWeight="Bold" HorizontalAlignment="Center" VerticalAlignment="Center"/>       
        </Grid>
    
    
    

    MainWindowViewModel.cs:

      public class MainWindowViewModel
        {
            public string ChartName { get; set; }
            public MainWindowViewModel()
            {
                ChartName = "Child";
            }
        }
    
    

    MainWindow.xaml:

    <Window x:Class="WpfApp1.MainWindow"
          ...
            xmlns:local="clr-namespace:WpfApp1"
            xmlns:views="clr-namespace:WpfApp1.Views"
            
            mc:Ignorable="d"
            Title="MainWindow" Height="450" Width="800">
        <Window.Resources>
         
            <views:MainWindowViewModel x:Key="_instance" />
        </Window.Resources>
    
        <StackPanel>
            <views:NewChart DataContext="{StaticResource _instance}" />
          
        </StackPanel>
    
     
    </Window>
    
    
    

    MainWindow.xaml.cs:

     public partial class MainWindow : Window
        {
            public MainWindowViewModel _instance { get; set; }
    
            public MainWindow()
            {
                InitializeComponent();
    
                
                _instance = new MainWindowViewModel();
                DataContext = _instance;
            }
    
        }
    
    
    

    The result:

    User's image


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.