简体   繁体   English

将DataContext设置为xaml.cs文件

[英]Setting DataContext to xaml.cs file

I've got a Grid and a component inside it. 我有一个Grid和其中的一个组件。 The Grid has a custom DataContext while the children has to use the default .xaml.cs file. Grid具有自定义DataContext而子级必须使用默认的.xaml.cs文件。

Of course, changing the DataContext for the parent control changes it also for the children. 当然,更改父控件的DataContext也会更改子控件的DataContext

So I need to set the children's DataContext to the xaml.cs file. 因此,我需要将孩子的DataContext设置为xaml.cs文件。

I'm trying using DataContext="{Binding}" but it's not working. 我正在尝试使用DataContext="{Binding}"但无法正常工作。

How can I do that? 我怎样才能做到这一点?

EDIT: Here's my code based on the replies 编辑:这是我的基于答复的代码

<UserControl x:Class="MyNamespace.MyClass"
x:Name="MyName"
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:lsp="clr-namespace:LSPlugins"
xmlns:utils="clr-namespace:LSPlugins.Utilities"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
d:DesignHeight="480" d:DesignWidth="480"> 

    <UserControl.Resources>
        <utils:ColorToSolidColorBrushValueConverter x:Key="ColorConverter"/>
        <lsp:MyModel x:Key="MyModel" x:Name="MyModel"/>
    </UserControl.Resources>

    <Grid x:Name="LayoutRoot" DataContext="{StaticResource MyModel}" Background="{Binding Path=BackgroundColor, Converter={StaticResource ColorConverter}}" Opacity="{Binding Path=BackgroundOpacity}">        
        <ContentPresenter Content="{Binding PresenterContent}" DataContext="{Binding ElementName=MyName}"/>
    </Grid>
</UserControl>

I've tried with both Name and x:Name but it's still not working and it throws this exception: 我已经尝试过同时使用Namex:Name但是它仍然无法正常工作,并且会引发以下异常:

System.Windows.Data Error: BindingExpression path error: 'PresenterContent' property not found on 'MyNamespace.MyModel' 'MyNamespace.MyModel' (HashCode=63183526). System.Windows.Data错误:BindingExpression路径错误:在'MyNamespace.MyModel''MyNamespace.MyModel'(HashCode = 63183526)上找不到'PresenterContent'属性。 BindingExpression: Path='PresenterContent' DataItem='MyNamespace.MyModel' (HashCode=63183526); BindingExpression:路径='PresenterContent'数据项='MyNamespace.MyModel'(HashCode = 63183526); target element is 'System.Windows.Controls.ContentPresenter' (Name=''); 目标元素是'System.Windows.Controls.ContentPresenter'(Name =''); target property is 'Content' (type 'System.Object').. 目标属性为“内容”(类型为“ System.Object”)。

Try by binding the page element itself to the DataContext property: 尝试将页面元素本身绑定到DataContext属性:

DataContext="{Binding ElementName=phoneApplicationPage}

Or, in the code-behind (ie xaml.cs file): 或者,在后面的代码中(即xaml.cs文件):

yourElement.DataContext = this;

EDIT: 编辑:

Or, you can set the Content with a Binding by setting the source there: 或者,您可以通过在其中设置源来设置带有BindingContent

Content="{Binding PresenterContent, ElementName=MyName}"

您可以命名父控件,然后使用ElementName绑定子控件的DataContext:

DataContext="{Binding ElementName=TheWindow}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM