SQL,WPF XAML中,C#,绑定数据,动态资源,访问非静态的数据,获得一个引用对象 [英] Sql, Wpf, Xaml, C#, Binding data, Dynamic resource, accessing to non-static data, Obtaining a Reference to an Object

查看:280
本文介绍了SQL,WPF XAML中,C#,绑定数据,动态资源,访问非静态的数据,获得一个引用对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,好吧,我pretty pretty pretty新的WPF和XAML,尽管我的搜索我无法找到一个简单的解决方案,在我看来,我将无法找到答案pretty很快。

Ok, well I am pretty pretty pretty new to WPF and XAML, despite my search I could not find a simple solution and it seems to me that I won't be able to find an answer pretty soon.

问题是如此简单,我创建了一个WPF项目,并在SelectList.xaml一个DataGrid一旦选定一排,我选定的行保存在对象说所谓的类别这个对象。到目前为止,一切都很好,但我无法弄清楚如何,我会从别的地方获得temp.xaml这个对象的引用?

The question is so simple, I have created a WPF project and have a datagrid in SelectList.xaml Once a row selected, I save the selected row in an object say this object called "category". So far everything is ok but I can't figure out how I am going to obtain a reference to this object from an other place temp.xaml ?

非常感谢
任何帮助将是非常美联社preciated
干杯

Thanks very much Any help will be highly appreciated Cheers

推荐答案

要提供WPF间接传播的常见方法是利用调解格局。您可以使用一个中介来发布你的类别的选择,并具备临时视图订阅更改通知你的类别的选择。

A common way to provide indirect communication in WPF is to leverage the Mediator pattern. You can use a mediator to publish the selection of your category, and have the temp view subscribe to notification of a change in selection of your category.

请参阅http://www.eggheadcafe.com/tutorials/aspnet/ec832ac7-6e4c-4ea8-81ab-7374d3da3425/wpf-and-the-model-view-vi.aspx对于一个具体的介体的一个简单的例子。还有,如果你想要一个更健壮的实现提供Mediator模式的实现提供了一些流行的MVVM框架。

See http://www.eggheadcafe.com/tutorials/aspnet/ec832ac7-6e4c-4ea8-81ab-7374d3da3425/wpf-and-the-model-view-vi.aspx for a simple example of a concrete mediator. There are also several popular MVVM frameworks available that provide Mediator pattern implementations if you want a more robust implementation.

简单中保实现:

public sealed class Mediator
{
    private static Mediator instance = new Mediator();
    private readonly Dictionary<string, List<Action<object>>> callbacks 
      = new Dictionary<string, List<Action<object>>>();

    private Mediator() { }

    public static Mediator Instance
    {
        get
        {
            return instance;
        }
    }

    public void Register(string id, Action<object> action)
    {
        if (!callbacks.ContainsKey(id))
        {
            callbacks[id] = new List<Action<object>>();
        }

        callbacks[id].Add(action);
    }

    public void Unregister(string id, Action<object> action)
    {
        callbacks[id].Remove(action);

        if (callbacks[id].Count == 0)
        {
            callbacks.Remove(id);
        }
    }

    public void SendMessage(string id, object message)
    {
        callbacks[id].ForEach(action => action(message));
    }
}

SelectList.xaml code-背后:

private void DataGrid_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
    var category = e.AddedItems.FirstOrDefault() as Category;

    if(category != null)
    {
        Mediator.Instance.SendMessage("Category Selected", category);
    }
}

Temp.xaml code-背后:

public Temp()
{
  InitializeComponent();

  Mediator.Instance.Register
  (
      "Category Selected",
      OnCategorySelected
  );
}

private void OnCategorySelected(object parameter)
{
  var selectedCategory = parameter as Category;

  if(selectedCategory != null)
  {
  }
}

这篇关于SQL,WPF XAML中,C#,绑定数据,动态资源,访问非静态的数据,获得一个引用对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆