user2877820
user2877820

Reputation: 307

MVVM - Mutliple classes in different projects for one control

I have this little piece of code right here. What I want to achieve is that the Command property is binded to Class 1 while the IsEnabled property is binded to Class 2.

<MenuItem Header="A_nmelden..." Command="{Binding ShowLoginCommand}" IsEnabled="{Binding Source={x:Static UserManagementAdapter.LogOnIsEnabled}}"/>

But there seems to be a problem when trying to access UserManagmentAdapter. The class UserManagmentAdapter is not in the same project as the project that contains this MenuItem control. So I am getting the error

"UserManagmentAdapter" is not supported in a Windows Presentation Foundation (WPF) project

My properties that I want to access:

private bool logOnIsEnabled;
public bool LogOnIsEnabled
{
    get { return this.logOnIsEnabled; }
    set { this.logOnIsEnabled = value; OnPropertyChanged("LogOnIsEnabled"); }
}

private bool logOffIsEnabled;
public bool LogOffIsEnabled
{
    get { return this.logOffIsEnabled; }
    set { this.logOffIsEnabled = value; OnPropertyChanged("LogOffIsEnabled"); }
}

My Class 2:

namespace ZF.UserManagement
{
    [ExportAdapter(nameof(UserManagementAdapter))]
    public class UserManagementAdapter : AdapterBase, IMultiValueConverter
    {...}
}

I hope I have made it clear enough. Anyone knows how to access UserManagmentAdapter?

Upvotes: 0

Views: 50

Answers (2)

mm8
mm8

Reputation: 169280

You need to add a namespace declaration in your XAML:

<MenuItem xmlns:local="clr-namespace:YourNamespace;assembly=YourAssembly" 
          Header="A_nmelden..." Command="{Binding ShowLoginCommand}" IsEnabled="{Binding Source={x:Static local:UserManagmentAdapter.LogOnIsEnabled}}"/>

...where "YourNamespace" is the namespace in which the UserManagmentAdapter class is defined and "YourAssembly" is the name of the assembly.

If the UserManagmentAdapter class is defined in your WPF application project, i.e. not in a separate assembly, you should remove the ;assembly=YourAssembly part:

xmlns:local="clr-namespace:ZF.UserManagement"

Upvotes: 1

Romano Zumb&#233;
Romano Zumb&#233;

Reputation: 8079

Why not make Class1 and Class2 properties of your ViewModel and then access the properties in XAML:

Code behind

public class ViewModel
{
    public Class1 class1 { get; set; }
    public Class2 class2 { get; set; }
}

XAML

<MenuItem Header="A_nmelden..." Command="{Binding class1.ShowLoginCommand}" IsEnabled="{Binding class2.LogOnIsEnabled}"/>

Upvotes: 0

Related Questions