I would like to know, if it is possible, to use simple C# CLR Objects as a Command Parameter in XAML, like I do it with MarkupExtensions.
For example, I use my own Markup Extension, which looks like this:
public class FormOpenExtension : MarkupExtension
{
public eForm e { get; set; }
public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
}
The use of the MarkupExtension in XAML looks like this:
CommandParameter="{gui:FormOpen e=Login}
In XAML, I have full intellisense support for the FormOpenExtension Object , compiling is no problem. So this works like a charm.
—————————————
Now I have a simple CLR Object, that for example looks like this:
namespace DB
{
[DataContract(IsReference = true)]
public partial class User
{
#region Primitive Properties
[Key][DataMember]
public virtual int ID
{
get;
set;
}
[DataMember]
public virtual string Name
{
get;
set;
}
}
}
I can import the namespace into XAML and use the object, including intellisense like this:
CommandParameter="{db:User ID=1, Name=Foo}
Visual Studio says, I am using the User object like a MarkupExtension, but it is none.
I fully understand the problem. The plain User Object is no Extension.
What I don’t get, is how to use the User Object as a Command Parameter, without writing my own Extension.
So here are my 2 concrete questions:
1.) Is it possible to use my own objects in XAML, without wrapping them into a Extension?
2.) If yes, what would be the correct syntax for a Command Parameter?
Thank you.
———————————-
Ok, thank you Dimitri. Here is the solution, as it worked for me now:
Step 1) Import the namespace
xmlns:db="clr-namespace:DB"
Step 2) Using the CLR object as a static resource.
<UserControl.Resources>
<db:User x:Key="User"/>
</UserControl.Resources>
Step 3) Pass the Object Key, in this case User, to the CommandParameter
CommandParameter="{StaticResource User}
Step 4) Bind the User Object to the desired controls in your UserConrol
Text="{Binding Source={StaticResource User}, Path=EMail, Mode=TwoWay}"
That’s it.
If i understand question, ObjectDataProvider can help to find solution.
p.s. what about simple declaring object as resource and then use it via
StaticResourceextension.