我正在开发一个Windows Phone项目。 我对xaml有一些不太了解。
这是:
<Page
x:Class="CitiBox.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:converters="using:CitiBox.Converters"
xmlns:views="using:CitiBox.Views"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
DataContext="{Binding Main, Source={StaticResource Locator}}">
<Page.Resources>
<converters:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
</Page.Resources>
<Grid>
<!--<TextBlock Text="Test1" Visibility="{Binding Test2, Converter={StaticResource BooleanToVisibilityConverter}}"/>
<TextBlock Text="Test2" Visibility="{Binding Test1, Converter={StaticResource BooleanToVisibilityConverter}}"/>-->
<views:LoadingView Visibility="{Binding Test2, Converter={StaticResource BooleanToVisibilityConverter}}"/>
<views:DrawerView Visibility="{Binding Test1, Converter={StaticResource BooleanToVisibilityConverter}}"/>
</Grid>
</Page>
如您所见,有两个注释的文本块和两个视图(用户控件)。您还可以看到我使用BooleanToVisibilityConverter将这四个元素的可见性绑定到两个布尔值。
令我惊讶的是,如果我使用两个文本块测试此代码,它可以正常工作,如果我尝试使用它没有的视图。我的观点只包含一个带有消息的文本块,没什么特别的。
这是他们的样子:
<UserControl
x:Class="CitiBox.Views.LoadingView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:CitiBox.Views"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400"
DataContext="{Binding Loading, Source={StaticResource Locator}}">
<Grid>
<TextBlock Text="Loading!!"/>
</Grid>
</UserControl>
你能知道它可能是什么吗? 提前致谢, 纪尧姆。
答案 0 :(得分:1)
在UserControl
中,您明确设置了DataContext
。
DataContext="{Binding Loading, Source={StaticResource Locator}}"
这意味着以下绑定不起作用:
Visibility="{Binding Test2, Converter={StaticResource BooleanToVisibilityConverter}}"
原因是因为绑定在UserControl的当前DataContext中寻找Test2
。相反,您需要在父控件的DataContext 中查找Test2
。
请改为尝试:
Visibility="{Binding DataContext.Test2, RelativeSource={RelativeSource AncestorType=Page}, Converter={StaticResource BooleanToVisibilityConverter}}"
答案 1 :(得分:1)
问题是使用DataContext。您的MainPage
有一个DataContext:
DataContext="{Binding Main, Source={StaticResource Locator}}"
LoadingView还有一个DataContext
所以,当你编写这段代码时:
您尝试在LoadingView的DataContext中找到Test2
作为字段,因为您处于同一个视图中。要避免此行为,您可以尝试:
<views:LoadingView Visibility="{Binding ElementName=Page,Path=DataContext.Test2, Converter={StaticResource BooleanToVisibilityConverter}}"/>
或者您可以编写一个名为NavigationService
的类继承自INavigationService
(MVVM Light)
项目中的每个视图都是一个页面,您将成为所有页面之间的过渡(如淡入淡出)
答案 2 :(得分:-1)
我认为您的问题是DataContext
因为Page DataContext是
DataContext="{Binding Main, Source={StaticResource Locator}}"
将usercontrol dataContext设置为DataContext="{Binding Loading, Source={StaticResource Locator}}"
的位置尝试更改您的代码
<Page x:Class="CitiBox.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:converters="using:CitiBox.Converters"
xmlns:views="using:CitiBox.Views"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
DataContext="{Binding Main, Source={StaticResource Locator}}"
x:Name="Page">
<Page.Resources>
<converters:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
</Page.Resources>
<Grid>
<!--<TextBlock Text="Test1" Visibility="{Binding Test2, Converter={StaticResource BooleanToVisibilityConverter}}"/>
<TextBlock Text="Test2" Visibility="{Binding Test1, Converter={StaticResource BooleanToVisibilityConverter}}"/>-->
<views:LoadingView Visibility="{Binding ElementName=Page,Path=DataContext.Test2, Converter={StaticResource BooleanToVisibilityConverter}}"/>
<views:DrawerView Visibility="{Binding ElementName=Page,Path=DataContext.Test1, Converter={StaticResourceBooleanToVisibilityConverter}}"/>
</Grid>
</Page>