Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在usercontrols中使用迁移值_C#_Wpf_Xaml_User Controls_Wpfdatagrid - Fatal编程技术网

C# 在usercontrols中使用迁移值

C# 在usercontrols中使用迁移值,c#,wpf,xaml,user-controls,wpfdatagrid,C#,Wpf,Xaml,User Controls,Wpfdatagrid,我有两个名为 欢迎光临 资料 在一个主窗口中 在第二个usercontrol中,我使用的是datagrid。在更改datagrid中的元素时,我必须能够在Welcome用户控件中设置值 private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e) { // Here on change of this event i must be able to display // da

我有两个名为

  • 欢迎光临
  • 资料
  • 在一个主窗口中

    在第二个usercontrol中,我使用的是datagrid。在更改datagrid中的元素时,我必须能够在
    Welcome
    用户控件中设置值

    private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        // Here on change of this event i must be able to display 
        // data in textbox i.e.txtClientName
    }
    
    欢迎
    用户控件中

    <StackPanel Orientation="Horizontal">
        <Label Content="Name:" FontWeight="Bold" Name="lblClientName" />
        <TextBox Name="txtClientName" Width="85" 
          Background="Transparent" IsReadOnly="True"/>
    </StackPanel>
    

    这是一件非常简单的事情,只需要一些.net framework的基本知识。。我希望这能帮助你。我刚刚在Data usercontrol中创建了一个自定义事件,它将冒泡行事件

    数据用户控制代码:

     public partial class Data : UserControl
        {
    
            private event EventHandler _RowSelectionChanged;
            public event EventHandler RowSelectionChanged
            {
                add { _RowSelectionChanged += value; }
                remove { _RowSelectionChanged -= value; }
            }
    
            private void RaiseSelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                if (_RowSelectionChanged != null)
                    _RowSelectionChanged(sender, e);
            }
            public Data()
            {
                InitializeComponent();
            }
    
            private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                RaiseSelectionChanged(sender, e);
            }
    
        }
    
     public partial class Welcome : UserControl
        {
            public Welcome()
            {
                InitializeComponent();
            }
    
            public string ClientName
            {
                get 
                {
                    return txtClientName.Text;
                }
                set
                {
    
                    txtClientName.Text = value;
    
                }
            }
        }
    
    欢迎用户控制代码:

     public partial class Data : UserControl
        {
    
            private event EventHandler _RowSelectionChanged;
            public event EventHandler RowSelectionChanged
            {
                add { _RowSelectionChanged += value; }
                remove { _RowSelectionChanged -= value; }
            }
    
            private void RaiseSelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                if (_RowSelectionChanged != null)
                    _RowSelectionChanged(sender, e);
            }
            public Data()
            {
                InitializeComponent();
            }
    
            private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                RaiseSelectionChanged(sender, e);
            }
    
        }
    
     public partial class Welcome : UserControl
        {
            public Welcome()
            {
                InitializeComponent();
            }
    
            public string ClientName
            {
                get 
                {
                    return txtClientName.Text;
                }
                set
                {
    
                    txtClientName.Text = value;
    
                }
            }
        }
    
    主窗口类别:

    public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
            }
    
            void MainWindow_Loaded(object sender, RoutedEventArgs e)
            {
                ucData.RowSelectionChanged += new EventHandler(ucData_RowSelectionChanged);
            }
    
            void ucData_RowSelectionChanged(object sender, EventArgs e)
            {
                var ev = e as SelectionChangedEventArgs;
                var grid = sender as DataGrid;
                ucWelcome.ClientName = "any thing";
                //this is how you can change Welcome UserControl
            }
        }
    

    注意。

    您可以在数据用户控件中创建自定义事件。并订阅它的事件,欢迎用户控件需要对其执行一些操作。在datagrid中的每一行单击,文本框的值(在欢迎用户控件中)都必须更改,我对此感到困惑。请任何人提供我示例代码吗