小编Jim*_*mas的帖子

如何使用StaticResource在XAML中定义DataContext

我想通过XAML中的静态资源声明一个DataContext,作为Northwind数据库中Customers的绑定.我可以在代码(C#)中轻松完成此操作,但想学习如何在XAML中完成.我已经尝试了所有可以找到的例子,但它们都不适合我.我认为这个问题出现在我标记为[Option1]和[Option2]的两行XAML代码行中.你能澄清一下这个语法应该是什么吗?

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();
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

XAML

 <Window x:Class="DataGridEF.MainWindow" …
Run Code Online (Sandbox Code Playgroud)

c# wpf datacontext xaml

7
推荐指数
1
解决办法
2万
查看次数

标签 统计

c# ×1

datacontext ×1

wpf ×1

xaml ×1