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.
我希望能够在XAML中为WPF StartupUri窗口创建DataContext对象的实例,而不是创建它的代码,然后编程地设置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 DataContext
object in XAML but I can't find it in any of the usual places...
我确定我已经读过了如何在XAML中实例化DataContext对象,但是我在任何通常的地方都找不到它。
4 个解决方案
#1
31
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名称空间,在窗口资源中创建它的实例,并将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>
#2
27
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。
#3
15
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!
最好的运气!
#4
0
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.
如果要分配一个不同类的实例。
#1
31
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名称空间,在窗口资源中创建它的实例,并将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>
#2
27
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。
#3
15
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!
最好的运气!
#4
0
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.
如果要分配一个不同类的实例。