如何使用StaticResource在XAML中定义DataContext [英] How to define DataContext in XAML using StaticResource
问题描述
我想通过XAML中的静态资源声明一个DataContext作为Northwind数据库中Customer的绑定。我可以在代码(C#)中轻松完成此操作,但想学习如何在XAML中进行操作。我已经尝试了所有可以找到的示例,但是没有一个适合我。我相信问题出在我已标记为[Option1]和[Option2]的两个XAML代码行中。您能否弄清楚该语法的真正含义是什么?
I want to declare a DataContext through a static resource in XAML as a binding for the Customers in the Northwind database. I can do this easily in code (C#) but want to learn how to do in XAML. I have tried all of the examples I can find but none of them work for me. I believe the issue is in the two XAML lines of code I have labeled [Option1] and [Option2]. Can you clarify what the syntax for this really should be?
C#
namespace DataGridEF
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
bModel1 bNorthWind = new bModel1();
//this.DataContext = bNorthWind;
bNorthWind.GetCustomers();
}
}
}
namespace DataGridEF
{
public class bModel1
{
List<Customer> _Customers;
public List<Customer> Customers
{
get { return _Customers; }
set { _Customers = value; }
}
public void GetCustomers()
{
NorthwindEntities NorthWind = new NorthwindEntities();
var CustomerQ = from cust in NorthWind.Customers select cust;
_Customers = CustomerQ.ToList();
}
}
}
XAML
<Window x:Class="DataGridEF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
xmlns:vm="clr-namespace:DataGridEF">
<Window.Resources>
<vm:bModel1 x:Key="TheViewModel" />
</Window.Resources>
<Grid>
<DataGrid AutoGenerateColumns="False" Height="195"
HorizontalAlignment="Left" Margin="20,89,0,0"
Name="dataGrid1" ItemsSource="{Binding Path=Customers}"
[option1]DataContext="{StaticResource TheViewModel}"
[option2]DataContext=
"{Binding Path=., Source={StaticResource TheViewModel}}"
VerticalAlignment="Top" Width="471" >
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Path=ContactName}" />
<DataGridTextColumn Header="Address" Binding="{Binding Path=Address}" />
<DataGridTextColumn Header="City" Binding="{Binding Path=City}" />
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
推荐答案
如果要避免将问题与Entities Framework和MSSQL复杂化NorthWind数据库,然后在代码项目 WPF / MVVM快速入门教程
If to avoid complicating the question with Entities Framework and MSSQL NorthWind database, then the good illustration is provided in Example2 sample code of codeproject "WPF/MVVM Quick Start Tutorial"
对于XAML,您应将其开头更改为:
For your XAML you should change the beginning of it to:
<Window x:Class="DataGridEF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
xmlns:vm="clr-namespace:DataGridEF">
<Window.DataContext>
<vm:bNorthWind />
</Window.DataContext>
<Grid>
<!---Couldnt check your code due to dependencies on
EF and MSSQL NorthWind database
See the reference for working illustration sample:
http://www.codeproject.com/Articles/165368/WPF-MVVM-Quick-Start-Tutorial
-->
</Grid>
</Window>
此方法的另一种形式可以在,部分:
Another variation of this approach can be seen in "What is the advantage of setting DataContext in code instead of XAML?", the part:
<StackPanel.DataContext>
<local:CustomerViewModel />
</StackPanel.DataContext>
将 DataContext
的定义从代码隐藏迁移到XAML是与 StaticResource
或 DynamicResource
的使用无关。请参阅: WPF中的StaticResource和DynamicResource有什么区别?可能在codeproject WPF:StaticResource vs.DynamicResource
Migrating DataContext
definition from codebehind to XAML is unrelated to the usage of either StaticResource
or DynamicResource
. See: What's the difference between StaticResource and DynamicResource in WPF? probably better addressed in codeproject WPF: StaticResource vs. DynamicResource
相关的,有用的和进一步的阅读内容:
Related, helpful and further reading:
- 如何摆脱DataContext InitializeComponent中的StackOverflow异常?
- 为什么在其中绑定MainWindow数据上下文XAML的行为与使用this.datacontext = this进行代码绑定中的行为相同?
- How to get rid of StackOverflow Exception in DataContext InitializeComponent?
- Why does binding the MainWindow datacontext in XAML fail to act the same as binding in the codebehind with this.datacontext=this?
这篇关于如何使用StaticResource在XAML中定义DataContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!