Sql、Wpf、Xaml、C#、绑定数据、动态资源、访问非静态数据、获取对象的引用

发布于 2024-10-05 06:55:51 字数 226 浏览 7 评论 0原文

好吧,我对 WPF 和 XAML 相当陌生,尽管我进行了搜索,但我找不到一个简单的解决方案,而且在我看来,我不会很快找到答案。

问题很简单,我创建了一个 WPF 项目,并在 SelectList.xaml 中有一个数据网格。一旦选择了一行,我将所选行保存在一个名为“类别”的对象中。到目前为止,一切正常,但我不知道如何从其他地方 temp.xaml 获取对该对象的引用?

非常感谢 任何帮助将不胜感激 干杯

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.

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 ?

Thanks very much
Any help will be highly appreciated
Cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

惯饮孤独 2024-10-12 06:55:52

在 WPF 中提供间接通信的常见方法是利用调解器模式。您可以使用调解器来发布类别选择,并让临时视图订阅类别选择更改的通知。

请参阅 http://www.eggheadcafe.com/tutorials/aspnet/ec832ac7-6e4c-4ea8-81ab-7374d3da3425/wpf-and-the-model-view-vi.aspx 有关具体调解器的简单示例。如果您想要更健壮的实现,还有几种流行的 MVVM 框架可以提供中介器模式实现。

简单调解器实现:

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 代码隐藏:

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 代码隐藏:

public Temp()
{
  InitializeComponent();

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

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

  if(selectedCategory != null)
  {
  }
}

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.

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.

Simple Mediator 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-behind:

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

public Temp()
{
  InitializeComponent();

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

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

  if(selectedCategory != null)
  {
  }
}
扮仙女 2024-10-12 06:55:52

创建一个可访问的方法(如果需要,可以公开),该方法接受“Temp.xaml”代码隐藏文件中此“类别”对象的引用。然后通过此方法将“category”对象从“SelectList.xaml”代码隐藏文件传递到“Temp.xaml”文件。

Create a accessibly method (public if you want to) which accepts the reference of this "category" object in the "Temp.xaml" code behind file. Then pass the "category" object from the "SelectList.xaml" code behind file to the "Temp.xaml" file through this method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文