wpf ,实现MainView中的UserControrl控件内的button的点击事件及属性引出来

背景

WPF在组件用户控件UserControl的应用中,对于UserControl的组件操作是必然的,然而,如何被UserControl内的操作事件引出来,由实际的业务窗体调用,就是当下的需要事件的案例。

技术案例

本次案例使用ToggleButton的自定义控件作为最小元素嵌入到UserControl中,在将

UserControl动态加载到WrapPanel内,实现WrapPanel内的元素点击事件被业务窗体接收。

技术点

自定义控件、组合控件、委托事件、动态加载、元素遍历;

wpf ,实现MainView中的UserControrl控件内的button的点击事件及属性引出来_第1张图片

代码

ChildItem




    

UserControl


    

        

    

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace UserControlEvent
{
    /// 
    /// UserControl1.xaml 的交互逻辑
    /// 
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }
        public event EventHandler MyButtonClicked;

        private void ChildItem_Checked(object sender, RoutedEventArgs e)
        {
            MyButtonClicked?.Invoke(this, EventArgs.Empty);
        }

        public void setContent(string str)
        {
            ccc.Content = str;
        }


        public string getContent()
        {
          return   ccc.Content.ToString();
        }


        public void reset()
        {
            ccc.IsChecked = false;
        }


        public bool getStatus()
        {
            return (bool)ccc.IsChecked;
        }
    }
}

MainWindow


    
        

        

    

using Lib.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace UserControlEvent
{
    /// 
    /// MainWindow.xaml 的交互逻辑
    /// 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            for (int i = 0; i < 5; i++)
            {
                UserControl1 Temp = new UserControl1();
                Temp.setContent("UserControl1" + i.ToString());
                Temp.Margin = new Thickness(5, 5, 5, 5);
                Temp.MyButtonClicked += MyUserControl_MyButtonClicked;
                TopBarPanel.Children.Add(Temp);
            }
        }

        private void MyUserControl_MyButtonClicked(object sender, System.EventArgs e)
        {
            UserControl1 btn = sender as UserControl1;
            foreach (UIElement element in TopBarPanel.Children)
            {
                if (element is UserControl1)
                {
                    UserControl1 item = (UserControl1)element;
                    if(item.getContent()!= btn.getContent())
                    {
                        item.reset();
                    }
                }
            }     
            //MessageBox.Show(btn.getContent());
        }



    }
}

你可能感兴趣的:(wpf)