绑定到另一个视图模型 [英] Binding to another viewmodel
问题描述
我试图将可见性属性绑定到在视图模型(MainViewModel
)中创建的函数,但是出现此错误:
I am trying to bind a visibility property to a function I made in a viewmodel (MainViewModel
), but I am getting this error:
mscorlib.dll中发生了类型为'System.IO.FileNotFoundException'的第一次机会异常 System.Windows.Data错误:BindingExpression路径错误:在'Locator''System.String'(HashCode = -191326816)上找不到'Main'属性. BindingExpression:Path ='Main.TilesHomeViewVisible'DataItem ='Locator'(HashCode = -191326816);目标元素是"myApp.Views.TilesHomeView"(名称="myTilesHomeView");目标属性为可见性"(类型为"System.Windows.Visibility").
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
然后呢?
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?
我集成了一个'ViewModelLocator'.
I have a 'ViewModelLocator' integrated .
这是我的ViewModelLocator:
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>();
}
}
...
我的App.xaml:
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>
在我的MainPage.xaml中,并在链接到该定位器的位置上,有:
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>
MainViewModel.cs
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的数据上下文定义如下:
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没有任何功能,因此显示为
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.
谢谢
推荐答案
问题是您可能正在将TilesHomeViewModel设置为TilesHomeView用户控件根目录上的DataContext(这意味着TilesHomeView元素的DataContext也是TilesHomeView).
这是解决此问题的两种可能的方法:
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)
-第二种解决方案是移动您在UserControl中将DataContext设置为TilesHomeViewModel的位置:将DataContext设置为User控件的LayoutRoot网格而不是根.
-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.
这篇关于绑定到另一个视图模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!