Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I call a call method from XAML in WPF?

Tags:

wpf

xaml

How do I call a call method from XAML in WPF?

like image 894
joh Avatar asked Feb 02 '10 17:02

joh


People also ask

How do you call a method in XAML WPF?

You Can create RelayCommand inheriting ICommand, and then create property of ICommand and assign relay command to that property and call the method.

Is WPF and XAML the same?

While XAML is used to build user interfaces in WPF, C# is used as the code-behind languages in WPF. While Windows and their controls are created in XAML at design-time, they can also be created at runtime using C# language. C# is also used to write all events programming and business logic.

How does XAML work with C#?

Forms application, XAML is mostly used to define the visual contents of a page and works together with a C# code-behind file. The code-behind file provides code support for the markup. Together, these two files contribute to a new class definition that includes child views and property initialization.

What is code-behind XAML?

Code-behind is a term used to describe the code that is joined with markup-defined objects, when a XAML page is markup-compiled. This topic describes requirements for code-behind as well as an alternative inline code mechanism for code in XAML.


2 Answers

The typical way this is handled is by wrapping your method into an ICommand, and using the Commanding infrastructure in WPF.

I blogged about Commanding, showing some of the advantages of this approach, especially when you use something like the RelayCommand implementation in Josh Smith's MVVM article.

like image 92
Reed Copsey Avatar answered Sep 29 '22 00:09

Reed Copsey


Except the commands there is another way that allows you to call a method directly from XAML. It is not commonly used but the option is still there.

The method must have one of the two types of signatures

  • void Foo()
  • void Bar(object sender, EventArgs e)

To make it working, you have to include references and namespaces of the Microsoft.Expression.Interactions and System.Windows.Interactivity into your project. Easiest way is to install a nuget. In the example below the namespaces are defined as xmlns:i and xmlns:ei.

<Window x:Class="Interactivity.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
    xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
    xmlns:local="clr-namespace:Interactivity"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">
<Grid>
    <Button HorizontalAlignment="Center" VerticalAlignment="Center" Content="Run" IsEnabled="{Binding CanRun}">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="Click">
                <ei:CallMethodAction MethodName="VoidParameterlessMethod" TargetObject="{Binding}" />
                <ei:CallMethodAction MethodName="EventHandlerLikeMethod" TargetObject="{Binding}" />
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </Button>
</Grid>

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;
    }

    public void VoidParameterlessMethod() 
        => Console.WriteLine("VoidParameterlessMethod called");

    public void EventHandlerLikeMethod(object o, EventArgs e) 
        => Console.WriteLine("EventHandlerLikeMethod called");

}
like image 45
chviLadislav Avatar answered Sep 28 '22 23:09

chviLadislav