2010-08-16 29 views
7

Tôi muốn khai báo một DataContext thông qua một tài nguyên tĩnh trong XAML như một ràng buộc cho các khách hàng trong cơ sở dữ liệu Northwind. Tôi có thể làm điều này dễ dàng trong mã (C#) nhưng muốn tìm hiểu làm thế nào để làm trong XAML. Tôi đã thử tất cả các ví dụ tôi có thể tìm thấy nhưng không ai trong số họ làm việc cho tôi. Tôi tin rằng vấn đề nằm trong hai dòng mã XAML mà tôi đã gắn nhãn [Option1] và [Option2]. Bạn có thể làm rõ cú pháp cho điều này thực sự nên là gì?Cách xác định DataContext trong XAML bằng cách sử dụng StaticResource

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> 

Trả lời

7

Nếu để tránh làm phức tạp thêm các câu hỏi với Entities Framework và cơ sở dữ liệu MSSQL NorthWind, sau đó hình minh họa tốt được cung cấp trong Ví dụ 2 mẫu mã của CodeProject "WPF/MVVM Quick Start Tutorial"

F hoặc XAML của bạn, bạn nên thay đổi ngay từ đầu của nó để:

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

Một biến thể của phương pháp này có thể được nhìn thấy trong "What is the advantage of setting DataContext in code instead of XAML?", phần:

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

Chuyển DataContext nét cao từ codebehind để XAML không liên quan đến việc sử dụng StaticResource hoặc DynamicResource. Xem: What's the difference between StaticResource and DynamicResource in WPF? có lẽ tốt hơn giải quyết trong CodeProject WPF: StaticResource vs. DynamicResource

liên quan, hữu ích và tiếp tục đọc:

1

tôi thích để thiết lập phím như là một chuỗi tĩnh - WPF có đủ chuỗi ma thuật mà không dồn vào góc refactoring nếu bạn có thể dễ dàng tránh nó.

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

trong App.xaml.cs

public static readonly string MainVmResourceKey = "MainVm"; 

trong nhiều Control.xaml

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

lưu ý của tôi phần UserControl là bất cứ loại bạn muốn áp dụng các ViewModel để.

Các vấn đề liên quan