如何在 XAML 中实例化 DataContext 对象

发布于 2024-08-06 23:27:38 字数 277 浏览 2 评论 0原文

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

主要原因是我不需要访问外部创建的对象,并且我不想仅仅为了设置 DataContext 而编写后面的代码。

我确定我已经在某处读过如何在 XAML 中实例化 DataContext 对象,但我无法在任何常用位置找到它......

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

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

花桑 2024-08-13 23:27:38

您可以为 DataContext 所在的任何命名空间添加 XML 命名空间,在窗口资源中创建它的实例并将 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>

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:

<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>
心如荒岛 2024-08-13 23:27:38

您可以直接在 XAML 中为整个窗口指定这一点:

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

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

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

<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.

戏蝶舞 2024-08-13 23:27:38

假设这段代码:

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

试试这个:

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

祝你好运!

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!

留一抹残留的笑 2024-08-13 23:27:38

如果您需要将 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>

使用relativesource绑定。

或只是

     <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 you need to set the DataContext as same control class:

    <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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文