anon
anon

Reputation: 49

sending parameter from XAML to C# method

Very new to XAML/C# and I wanted to know how to send a parameter from XAML to a C# method.

This TextBlock <TextBlock PreviewMouseDown="changeFont" /> would call a method with the signature changeFont(object sender, MouseButtonEventArgs e)

But I wanted to know if I could add a parameter to the signature like this: changeFont(object sender, MouseButtonEventArgs e, string Name)

and send an argument via XAML <TextBlock PreviewMouseDown="changeFont('Bob')" />

Upvotes: 1

Views: 1203

Answers (3)

Paboka
Paboka

Reputation: 442

You should use Command and CommandParameter properties. It's better to use MVVM pattern and link to a command from your ViewModel, but if you don't use it, you also can use the Routed Command system and set an event handler for the command via CommandBinding property:

<Button Content="Bob button"
        Command="{Binding ChangeFont}"
        CommandParameter="Bob" />

I used <Button /> here, but if you want it to look like a <TextBlock /> you can change its appearance by changing it's Template property. But remember, that user expects that a button behaves like a button while a text block behaves like a text block.

Upvotes: 0

Keith Stein
Keith Stein

Reputation: 6724

There is no way to do this in WPF; that's not how it's designed. If you need additional information to process the PreviewMouseDown event, you'll have to get it from somewhere else.

sender gives you the element that raised the event (in your case the TextBlock). So one option is to use the Tag property:

<TextBlock PreviewMouseDown="changeFont" Tag="Bob"/>
changeFont(object sender, MouseButtonEventArgs e)
{
    var moreInfo = ((TextBlock)sender).Tag;

    ...
}

One other thing to look into is the use of commands. Commands let you add a CommandParameter. Certain controls Command property that can be set to execute said command when they are interacted with, like when a Button is clicked. This isn't the case for TextBlock, but it might be useful later on.

Upvotes: 2

Nico Schertler
Nico Schertler

Reputation: 32597

No, this is not possible. The XAML snippet PreviewMouseDown="changeFont" attaches an event handler to the PreviewMouseDown event. And the event handler changeFont needs to have a fixed signature. You cannot simply add a parameter.

That being said, changeFont does not sound like the name of an event handler anyway. It is an action. If you have multiple of these text blocks and you want to re-use the changeFont method, you could define separate event handlers that each call the action:

<TextBlock PreviewMouseDown="BobPreviewMouseDown" />
<TextBlock PreviewMouseDown="AlicePreviewMouseDown" />
void BobPreviewMouseDown(object sender, MouseButtonEventArgs e)
{
    changeFont("Bob");
}

void AlicePreviewMouseDown(object sender, MouseButtonEventArgs e)
{
    changeFont("Alice")
}

That's one option. Of course, there are many others. You could, e.g., also have a single event handler and derive the parameter from the sender object (e.g. with an attached property or similar).

Upvotes: 1

Related Questions