问题
I am trying to bind a visibility property to a function I made in a viewmodel (MainViewModel
), but I am getting this error:
A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll System.Windows.Data Error: BindingExpression path error: 'Main' property not found on 'Locator' 'System.String' (HashCode=-191326816). BindingExpression: Path='Main.TilesHomeViewVisible' DataItem='Locator' (HashCode=-191326816); target element is 'myApp.Views.TilesHomeView' (Name='myTilesHomeView'); target property is 'Visibility' (type 'System.Windows.Visibility')..
From what I understand from the error, it is looking for the TilesHomeViewVisible
function in the TilesHomeViewModel
, while it is actually in the MainViewModel
. In the binding expression, how do I target the MainViewModel
then?
EDIT: I have a 'ViewModelLocator' integrated .
Here is my ViewModelLocator:
...
public ViewModelLocator()
{
ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
SimpleIoc.Default.Register<MainViewModel>();
SimpleIoc.Default.Register<TilesHomeViewModel>();
}
public MainViewModel Main
{
get
{
return ServiceLocator.Current.GetInstance<MainViewModel>();
}
}
public TilesHomeViewModel TilesVM
{
get
{
return ServiceLocator.Current.GetInstance<TilesHomeViewModel>();
}
}
...
My App.xaml:
<?xml version="1.0" encoding="utf-8"?>
<Application x:Class="myApp.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:app="clr-namespace:myApp" mc:Ignorable="d"
xmlns:views="clr-namespace:myApp.Views"
xmlns:vm="clr-namespace:myApp.ViewModels">
<!--Application Resources-->
<Application.Resources>
<ResourceDictionary>
<vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary>
<app:ColorWrapper x:Key="ColorWrapper" />
</ResourceDictionary>
<ResourceDictionary x:Name="ResourceDictionary1" Source="ResourceDictionary1.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
<Application.ApplicationLifetimeObjects>
<!--Required object that handles lifetime events for the application-->
<shell:PhoneApplicationService Launching="Application_Launching" Closing="Application_Closing" Activated="Application_Activated" Deactivated="Application_Deactivated" />
</Application.ApplicationLifetimeObjects>
</Application>
In my MainPage.xaml and where the linking to the locator was made , I have:
<phone:PhoneApplicationPage
...
d:DataContext="{d:DesignData SampleData/MainViewModelSampleData.xaml}"
...
>
<phone:PhoneApplicationPage.DataContext>
<Binding Path="Main" Source="{StaticResource Locator}"/>
</phone:PhoneApplicationPage.DataContext>
...
<Grid x:Name="LayoutRoot">
...
<local:TilesHomeView x:Name="myTilesHomeView" Visibility="{Binding Main.TilesHomeViewVisible,Source=Locator}" />
</Grid>
</phone:PhoneApplicationPage>
The MainViewModel.cs
public class MainViewModel : ViewModelBase
{
public MainViewModel()
{
this.Items = new ObservableCollection<ItemViewModel>();
}
Visibility _tilesHomeViewVisible = System.Windows.Visibility.Collapsed;
public Visibility TilesHomeViewVisible
{
get { return System.Windows.Visibility.Collapsed; }
set { _tilesHomeViewVisible = value; RaisePropertyChanged("TilesHomeViewVisible"); }
}
public void TilesHomeViewClose()
{
TilesHomeViewVisible = System.Windows.Visibility.Collapsed;
}
public bool IsDataLoaded
{
get;
private set;
}
/// <summary>
/// Creates and adds a few ItemViewModel objects into the Items collection.
/// </summary>
public void LoadData()
{...}
}
TilesHomeView.xaml has it data context defined as so:
<UserControl x:Class="myApp.Views.TilesHomeView"
....
DataContext="{Binding TilesVM, Source={StaticResource Locator}}"
>
<Grid x:Name="HomeGrid">
</Grid>
</UserControl>
HomeViewModel.cs is has no function and is presented as such
namespace myApp
{
public class TilesHomeViewModel : ViewModelBase
{
public TilesHomeViewModel()
{
}
}
}
I hope this is as detailed as possible. I really hope to find a solution to this error, it's been bugging me for days now.
Thanks
回答1:
The problem is that you are probably you are setting TilesHomeViewModel as the DataContext on the root of your TilesHomeView usercontrol (so that mean that the DataContext of TilesHomeView element is also TilesHomeView ).
Here is two possible solution to this problem:
When setting the the visibility explicitely set the source :
<local:TilesHomeView x:Name="myTilesHomeView" Visibility="{Binding Main.TilesHomeViewVisible,Source={StaticResource Locator} }" />
(Update with the appropriate value on your locator)
-A second solution is to move where you set the DataContext to TilesHomeViewModel in your UserControl: set the DataContext the LayoutRoot grid of the User control instead of the root.
来源:https://stackoverflow.com/questions/19206902/binding-to-another-viewmodel