O
O
Oksana Volovik2014-10-15 19:19:22
WPF
Oksana Volovik, 2014-10-15 19:19:22

How to pass a link to a parameter using xaml?

Actually, you need exactly what is in the question.
I can pass meaningful parameters, it's easy:

<ObjectDataProvider x:Key="userDCustom" ObjectType="{x:Type local:UserData}">
            <ObjectDataProvider.ConstructorParameters>
                <system:String>myName</system:String>
            </ObjectDataProvider.ConstructorParameters>
        </ObjectDataProvider>

But things get more complicated when you need to pass a reference parameter. For example, there is a UserData
class that takes a simple string in the constructor:
public class UserData
    {
        public string Name { get; set; }
        public UserData ( string name )
        {
            Name = name;
        }
    }

And there is a User class serving it , which takes an instance of the previous class as a parameter:
public class User
    {
        UserData _uData;

        public UserData UserData
        {
            get { return _uData; }
            private set { _uData = value; }
        }

        public User ( UserData data )
        {
            UserData = data;
        }
    }

How can I pass an instance reference to ObjectDataProvider.ConstructorParameters ?
With binding Source={...} , as usual, it doesn't work. Is it possible to do this at all, or do I need to look for workarounds?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sumor, 2014-10-15
@Sumor

It may be worth abandoning constructors with parameters and setting parameters as properties.
More or less like this:

<ObjectDataProvider x:Key="userDCustom" ObjectType="{x:Type local:UserData}">
            <ObjectDataProvider.ConstructorParameters>
                <local:User>
                      <local:User.UserData>
                             <local.User Name="MyName" />
                      </local:User.UserData>
                </local:User>
            </ObjectDataProvider.ConstructorParameters>
</ObjectDataProvider>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question