2010-08-16 46 views
7

我想通過XAML中的靜態資源聲明DataContext作爲Northwind數據庫中Customers的綁定。我可以在代碼(C#)中輕鬆完成此操作,但希望學習如何在XAML中執行此操作。我嘗試了所有我能找到的例子,但沒有一個適合我。我相信這個問題出現在我標記爲[Option1]和[Option2]的兩個XAML代碼行中。你能澄清一下這個語法應該是什麼嗎?如何使用StaticResource在XAML中定義DataContext

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> 

回答

7

如果以避免與實體框架和MSSQL NorthWind數據庫的問題複雜化,則很好地說明了在CodeProject上的示例2示例代碼提供「WPF/MVVM Quick Start Tutorial

F或者你的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.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> 

這種方法的另一個變化可以在"What is the advantage of setting DataContext in code instead of XAML?"可以看出,部分:

<StackPanel.DataContext> 
     <local:CustomerViewModel /> 
    </StackPanel.DataContext> 

遷移DataContext定義從代碼隱藏於XAML無關使用StaticResourceDynamicResource。請參閱:What's the difference between StaticResource and DynamicResource in WPF?在CodeProject上可能更好地解決WPF: StaticResource vs. DynamicResource

相關,樂於助人,進一步閱讀:

1

我喜歡設置密鑰爲靜態字符串 - WPF有如果你可以輕鬆地避開它,那麼沒有足夠的魔力而不會陷入重構的角落。

App.xaml

xmlns:viewModels="clr-namespace:MyAppNamespace.ViewModels" 
xmlns:local="clr-namespace:tvCADdesktop" 
x:Name="App" 
... 
<viewModels:ApplicationViewModel x:Key= "{x:Static local:App.MainVmResourceKey}"/> 

App.xaml.cs

public static readonly string MainVmResourceKey = "MainVm"; 

在我的各種Control.xaml

<UserControl.DataContext> 
    <Binding> 
     <Binding.Source> 
      <StaticResource ResourceKey="{x:Static app:App.MainVmResourceKey}" /> 
     </Binding.Source> 
    </Binding> 
</UserControl.DataContext> 

注意UserControl部分是要應用視圖模型到任何類型。

相關問題