【问题标题】:How to instantiate DataContext object in XAML如何在 XAML 中实例化 DataContext 对象
【发布时间】:2010-12-04 01:11:13
【问题描述】:

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

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

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

【问题讨论】:

    标签: c# wpf xaml datacontext


    【解决方案1】:

    您为 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>
    

    【讨论】:

    • 这在 .NET 框架 4.5 中有效吗?我试过这段代码,它说... The type 'local:MyViewModel' was not found. Verify that you are not missing an assembly and that all referenced assemblies have been built.
    • @RafafTahsin 你确定你有正确的命名空间吗?此外,如果您将引用的类型添加到项目中但尚未构建,则 WPF 设计器会有些困惑。你试过建造吗?
    【解决方案2】:

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

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

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

    【讨论】:

      【解决方案3】:

      假设这段代码:

      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}" />
      

      祝你好运!

      【讨论】:

      • +1 因为这显示了运行和设计时间,并且不使用 x:Key 而是将 DataContext 直接放在它所属的元素中
      【解决方案4】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-02-05
        • 1970-01-01
        • 2011-01-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多