在 XAML 页面级别定义的 UserControl DataContext
UserControl DataContext defined at XAML page level
我在 XAML 页面级别为 UserControl 定义了一个 DataContext,如下所示(最后一行是相关的行):
<UserControl
x:Class="Sample.MyUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:data="using:Sample.Models"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="768"
d:DesignWidth="1024"
DataContext="data:TestDataCollection">
目标是能够访问托管 UserControl 的页面中的 DataContext 对象,如下所示,myUserControl 是 UserControl 的 x:Name
。
TestDataCollection tdc = myUserControl.DataContext as TestDataCollection;
数据绑定一切正常,UI 在 UWP 平台上按预期显示和更新。
唯一的问题是上面的代码行没有返回预期的 DataContext 对象。事实上,myUserControl.DataContext 在调试过程中显示一个值为 "data:TestDataCollection" 的字符串(与上面的 XAML 代码相同)而不是 TestDataCollection
.[=16= 类型的对象]
这是另一件奇怪的事情:如果我将代码隐藏中的 DataContext 设置为:
this.DataContext = new TestDataCollection();
问题消失了,即 (myUserControl.DataContext 作为 TestDataCollection) returns 预期的 DataContext 对象。
我在 XAML 中设置页面 DataContext 做错了什么?
通过使用 DataContext="data:TestDataCollection"
,您只是设置了一个 string 值。如果要设置视图模型 object,则必须使用以下语法:
<UserControl
x:Class="Sample.MyUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:data="using:Sample.Models"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="768"
d:DesignWidth="1024">
<UserControl.DataContext>
<data:TestDataCollection />
</UserControl.DataContext>
</UserControl>
请注意,您的用户控件还将从使用它的页面继承 DataContext
。所以在大多数情况下,没有必要在您的控件中显式设置它,而您仍然可以在代码后面访问它(因为它是由页面隐式设置的)。
我在 XAML 页面级别为 UserControl 定义了一个 DataContext,如下所示(最后一行是相关的行):
<UserControl
x:Class="Sample.MyUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:data="using:Sample.Models"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="768"
d:DesignWidth="1024"
DataContext="data:TestDataCollection">
目标是能够访问托管 UserControl 的页面中的 DataContext 对象,如下所示,myUserControl 是 UserControl 的 x:Name
。
TestDataCollection tdc = myUserControl.DataContext as TestDataCollection;
数据绑定一切正常,UI 在 UWP 平台上按预期显示和更新。
唯一的问题是上面的代码行没有返回预期的 DataContext 对象。事实上,myUserControl.DataContext 在调试过程中显示一个值为 "data:TestDataCollection" 的字符串(与上面的 XAML 代码相同)而不是 TestDataCollection
.[=16= 类型的对象]
这是另一件奇怪的事情:如果我将代码隐藏中的 DataContext 设置为:
this.DataContext = new TestDataCollection();
问题消失了,即 (myUserControl.DataContext 作为 TestDataCollection) returns 预期的 DataContext 对象。
我在 XAML 中设置页面 DataContext 做错了什么?
通过使用 DataContext="data:TestDataCollection"
,您只是设置了一个 string 值。如果要设置视图模型 object,则必须使用以下语法:
<UserControl
x:Class="Sample.MyUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:data="using:Sample.Models"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="768"
d:DesignWidth="1024">
<UserControl.DataContext>
<data:TestDataCollection />
</UserControl.DataContext>
</UserControl>
请注意,您的用户控件还将从使用它的页面继承 DataContext
。所以在大多数情况下,没有必要在您的控件中显式设置它,而您仍然可以在代码后面访问它(因为它是由页面隐式设置的)。