有什么方法可以通过XAML管理IDisposable对象? [英] Is there any way to manage an IDisposable object from XAML?

查看:66
本文介绍了有什么方法可以通过XAML管理IDisposable对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个XAML对象,它是一个ObservableCollection,绑定到数据模型以使列表与服务器保持一致:

I have a XAML object, it is an ObservableCollection that is bound to a data model to keep the list reconciled with the server:

class MyCollection : ObservableCollection<OrderType>
{
    MyCollection()
    {
        DataModel.OrderType.OrderTypeRowChanged += new DataModel.OrderTypeRowChangedEventHandler(this.myhandler);
    }
}

我们如何在XAML中使用这样的对象?当您控制对象的生存期时,它非常容易,您可以使用IDisposable接口并将逻辑包装在"using"块中,或者通过"Unloaded"事件将其显式处理.然而, 我有绑定到这些列表的ItemsControl,这些控件必须与控件下方的数据模型自动协调.

How are we to use an object like this in XAML?  Its easy enough when you control the lifetime of the object, you can use the IDisposable interface and wrap the logic in a 'using' block or dispose of it explicitly with the 'Unloaded' event.  However, I've got ItemsControls that are bound to these lists that must be reconciled automatically with the data model underneath the controls.

有什么想法吗?

唐纳德·罗伊·艾里(Donald Roy Airey)

Sincerely, Donald Roy Airey

推荐答案

唐纳德

   

    

开始一个新的WPF项目并将其粘贴到:

Start a new WPF project and paste this in:

  

  

MainWindow.xaml

<Window
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	x:Class="WpfApplication10.MainWindow"
	x:Name="Window"
	Title="MainWindow"
	Width="640" Height="480"
    xmlns:local="clr-namespace:WpfApplication10">

	<StackPanel x:Name="LayoutRoot">
		<ListBox x:Name="list1" HorizontalAlignment="Left" Height="100" VerticalAlignment="Top" Width="100" ItemTemplate="{DynamicResource DataTemplate1}" >
			<ListBox.Resources>
				<DataTemplate x:Key="DataTemplate1">
					<local:MyButton Content="{Binding Name}"/>
				</DataTemplate>
			</ListBox.Resources>
		</ListBox>
		<Button Content="Test" Click="Button_Click" HorizontalAlignment="Left" Width="100" />
	</StackPanel>
</Window>

MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;

namespace WpfApplication10
{
    class MyClass : IDisposable
    {
        public string Name { get; set; }

        public void Dispose()
        {
            MessageBox.Show("Bye!");
        }
    }

    public class MyButton : Button
    {
        object myItem;

        public MyButton()
        {
            Unloaded += new RoutedEventHandler(MyButton_Unloaded);
            Loaded += new RoutedEventHandler(MyButton_Loaded);
        }

        void MyButton_Loaded(object sender, RoutedEventArgs e)
        {
            myItem = DataContext;
        }

        void MyButton_Unloaded(object sender, RoutedEventArgs e)
        {
            var dc = myItem as IDisposable;
            dc.Dispose();
        }
    }
    
    public partial class MainWindow : Window
	{

                public MainWindow()
		{
			this.InitializeComponent();
                        list1.ItemsSource = new List<MyClass> { new MyClass { Name = "one" }, new MyClass { Name = "two" } };
		}

        private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            list1.ItemsSource = null;
        }

    }
}

   

如果我对您的理解正确,那就可以将它们绑在一起.

   

If I understand you correctly, that is how you can tie it all together.

   

    

此致,
皮特

Regards,
Pete


这篇关于有什么方法可以通过XAML管理IDisposable对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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