C# 如何在 XAML 中实例化 DataContext 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1521699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:22:36  来源:igfitidea点击:

How to instantiate DataContext object in XAML

c#wpfxamldatacontext

提问by Gus Paul

I want to be able to create an instance of the DataContextobject for my WPF StartupUri window in XAML, as opposed to creating it code and then setting the DataContextproperty programmaticly.

我希望能够DataContext在 XAML 中为我的 WPF StartupUri 窗口创建对象的实例,而不是创建它的代码然后以DataContext编程方式设置属性。

The main reason is I don't need to access the object created externally and I don't want to have to write code behind just for setting the DataContext.

主要原因是我不需要访问外部创建的对象,并且我不想为了设置DataContext.

I'm sure I've read somewhere how to instantiate the DataContextobject in XAML but I can't find it in any of the usual places...

我确定我在某处读过如何DataContext在 XAML 中实例化对象,但我在任何常见的地方都找不到它......

采纳答案by Steven Robbins

You add an XML namespace for whatever namespace your DataContext lives in, create an instance of it in the Window Resources and set the DataContext to that resource:

您为 DataContext 所在的任何命名空间添加一个 XML 命名空间,在 Window Resources 中创建它的实例并将 DataContext 设置为该资源:

<Window x:Class="WpfApplication4.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication4"
    Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <local:MyViewModel x:Key="MyViewModel"/>
    </Window.Resources>
    <Grid DataContext="{StaticResource MyViewModel}">

    </Grid>
</Window>

回答by Reed Copsey

You can just specify this directly in XAML for the entire Window:

您可以直接在 XAML 中为整个窗口指定此项:

<Window 
    ... xmlns definitions ...
>
   <Window.DataContext>
        <local:CustomViewModel />
   </Window.DataContext>
</Window>

This creates a view model named "CustomViewModel" in the namespace aliased to local, directly as the DataContext for the Window.

这将在别名为 local 的命名空间中创建一个名为“CustomViewModel”的视图模型,直接作为窗口的 DataContext。

回答by Jerry Nixon

Assuming this code:

假设这段代码:

public abstract class BaseView { }
public class RuntimeView : BaseView { }
public class DesigntimeView : BaseView { }

Try this:

尝试这个:

<Page.DataContext>
    <local:RuntimeView />
</Page.DataContext>
<d:Page.DataContext>
    <local:DesigntimeView />
</d:Page.DataContext>
<ListBox ItemsSource="{Binding}" />

Best of luck!

祝你好运!

回答by Kylo Ren

If you need to set the DataContext as same control class:

如果您需要将 DataContext 设置为相同的控件类:

    <Window x:Class="TabControl.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        
            xmlns:local="clr-namespace:TabControl"
            Title="MainWindow" Height="350" Width="525"
            DataContext="{Binding RelativeSource={RelativeSource Self}}"        
            >
</Window>

use RelativeSource binding.

使用RelativeSource 绑定。

or just

要不就

     <Window x:Class="TabControl.MainWindow"
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        
                xmlns:local="clr-namespace:TabControl"
                Title="MainWindow" Height="350" Width="525"                        
                >
<Window.DataContext>
< new instance of any viewModel here....>
</Window.DataContext>
    </Window>

If want to assign an instance of different class than itself.

如果要分配与自身不同的类的实例。