Get the WPF Window handle

using System;
using System.Windows;

using System.Windows.Interop; // WindowInteropHelper


namespace WindowsApplication1
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>

    public partial class Window1 : System.Windows.Window
    {
        public Window1()
        {
            try
            {
                InitializeComponent();
                this.SourceInitialized += new EventHandler(Window1_SourceInitialized);
            }
            catch(Exception exp)
            {
                string str = exp.Message;
            }

        }

        void Window1_SourceInitialized(object sender, EventArgs e)
        {
            WindowInteropHelper helper = new WindowInteropHelper(this);
            IntPtr ptr = helper.Handle;
        }

        //private void ClickBtn(object sender, EventArgs e)
        //{
        //}
    }
}
### WPF-UI Framework Usage Examples and Features #### Data Binding in WPF WPF provides robust data binding capabilities, allowing developers to bind UI elements directly to data sources. For instance, the `DataGrid` control supports advanced editing scenarios through its integration with the `IEditableObject` interface[^2]. When implementing custom business objects, one must explicitly define how these objects handle transactions by implementing this interface. Here’s an example demonstrating basic data binding using a simple ViewModel: ```csharp public class Person : INotifyPropertyChanged, IEditableObject { private string _name; public string Name { get => _name; set { if (_name != value) { _name = value; OnPropertyChanged(nameof(Name)); } } } public void BeginEdit() { // Save current state before edits begin. } public void CancelEdit() { // Restore original values upon cancellation. } public void EndEdit() { // Commit changes after successful edit completion. } public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } ``` This code snippet illustrates how to create a model that adheres to both `INotifyPropertyChanged` for property change notifications and `IEditableObject` for transactional updates within grids like `DataGrid`. #### Event Aggregation Pattern in WPF Applications For decoupled communication between components in large-scale applications built on top of WPF, leveraging patterns such as Event Broker from Composite UI Application Block (CAB) or Event Aggregator from Prism becomes essential[^1]. These mechanisms allow loosely coupled parts of an application to communicate without direct references to each other. Below is an illustrative implementation utilizing Prism's `EventAggregator` service: ```csharp using Prism.Events; // Define a Pub/Sub message type public class MessagePublishedEvent : PubSubEvent<string> { } // Publisher side logic var ea = Container.Resolve<IEventAggregator>(); ea.GetEvent<MessagePublishedEvent>().Publish("Message sent!"); // Subscriber side logic ea.GetEvent<MessagePublishedEvent>().Subscribe(message => { Console.WriteLine($"Received: {message}"); }); ``` In this case, publishers send messages while subscribers listen for them asynchronously, promoting separation of concerns across modules. #### Custom Controls Development Developers often extend default controls provided out-of-the-box by creating derived classes tailored specifically towards their needs. Below shows extending Button behavior adding extra properties useful during runtime configuration dynamically. ```xml <Window x:Class="CustomControlExample.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <StackPanel> <!-- Example usage --> <local:ExtendedButton Content="Click Me!" IsEnabledDynamicBinding="{Binding Path=SomeCondition}" /> </StackPanel> </Window> <!-- Code Behind Definition --> public class ExtendedButton : Button { public static readonly DependencyProperty IsEnabledDynamicBindingProperty = DependencyProperty.RegisterAttached( "IsEnabledDynamicBinding", typeof(bool), typeof(ExtendedButton)); public bool IsEnabledDynamicBinding { get { return (bool)this.GetValue(IsEnabledDynamicBindingProperty); } set { this.SetValue(IsEnabledDynamicBindingProperty, value); } } } ``` Such extensions provide flexibility enabling richer interactions based entirely around declaratively defined attributes inside XAML markup files themselves rather than procedural coding approaches elsewhere outside views altogether.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值