Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
454 views
in Technique[技术] by (71.8m points)

wpf - XAML method name as string - TypeConverter cannot convert from System.String

XAML has a nice way of allowing to use method names to set a method as a property - it uses a similar logic to assign event handlers after all, such as:

<!-- MyControl.xaml -->
<MyControl Event="MyControl_Event"/>
------------------------------------------
// MyControl.xaml.cs
private void MyControl_Event(object sender, EventArgs e) { ... }

As such, this pattern can be used to assign methods to Action and Func properties, such as:

<!-- MainWindow.xaml -->
<Window.DataContext>
    <local:SomeClass MyAction="MainWindow_Action"/>
</Window.DataContext>
-----------------------------------------------------
// SomeClass.cs
public class SomeClass
{
    public Action MyAction { get; set; }
}

// MainWindow.xaml.cs
private void MainWindow_Action() { ... }

This is fully functionnal in the application itself, but gives an error in visual studio (in the designer in particular): TypeConverter cannot convert from System.String. That error sounds like the designer fails to recognize the method name as a valid method of MainWindow, which is rather unsettling considering that, once I launch the application, everything works fine and as intended.

How can I fix this error without resorting to changing MyAction to a dependency property or an event?

question from:https://stackoverflow.com/questions/65598641/xaml-method-name-as-string-typeconverter-cannot-convert-from-system-string

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...