我试图可见性属性,以我在一个视图模型(MainViewModel
)做了一个功能绑定,但我得到这个错误:绑定到另一个视图模型
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')..
从我从错误理解,它正在寻找TilesHomeViewModel
中的TilesHomeViewVisible
函数,而它实际上在MainViewModel
中。在绑定表达式中,我该如何定位MainViewModel
?
编辑:我有一个'ViewModelLocator'集成。
这里是我的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>();
}
}
...
我的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>
在我MainPage.xaml中和其中连接到定位器被做,我有:
<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有它定义为这样的数据上下文:
<UserControl x:Class="myApp.Views.TilesHomeView"
....
DataContext="{Binding TilesVM, Source={StaticResource Locator}}"
>
<Grid x:Name="HomeGrid">
</Grid>
</UserControl>
HomeViewModel.cs是没有功能和呈现为这样
namespace myApp
{
public class TilesHomeViewModel : ViewModelBase
{
public TilesHomeViewModel()
{
}
}
}
我希望这是尽可能详细。我真的很希望找到解决这个错误的方法,现在它已经让我烦恼了好几天。
感谢
发布页面的.xaml,需要的是绑定的上下文,而不是绑定定义。 – lisp
请将后面的代码贴出 – Florian
我已更新我的帖子,以更多代码来详细说明情况。请看一看。谢谢 – JNate