【问题标题】:Get data from a user control in wpf to an user control?从wpf中的用户控件获取数据到用户控件?
【发布时间】:2015-03-21 04:07:02
【问题描述】:

基本上我是 WPF 的新手。我有一个用户控件 - A。在 A 中,我有另一个用户控件 B。当按下 B 上的按钮时,一个值将传递给 A。我正在尝试 WPF MVVM。请帮助我。

//-------------------MainWindow------------------//
public partial class MainWindow : Window
{
    public delegate void ValuePassDelegate(int ValueToGet);
    public event ValuePassDelegate ValuePassEvent;

    public UserControl1 UserControl1Obj = new UserControl1();
    public UserControl2 UserControl2Obj = new UserControl2();     
    public MainWindow()
    {
        InitializeComponent();
        ValuePassEvent += new ValuePassDelegate(method1);
        UserControl1Obj.del = ValuePassEvent;
    }
    public void method1(int ValueToGet)
    {
        UserControl2Obj.txtName.Text = ValueToGet.ToString();
    }
}

//---------------------UserControl1------------------//
public partial class UserControl1 : UserControl
{
    public Delegate del;
    public int ValueToPass = 0;
    public UserControl1()
    {
        InitializeComponent();
    }
    public void method1()
    {
        del.DynamicInvoke(ValueToPass);
    }
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        method1();
    }
}

//-----------------UserControl2--------------------//
 public partial class UserControl2 : UserControl
{
    public int ValueToGet;
    public UserControl2()
    {
        InitializeComponent();

    }
}

这些是 UserControl1、UserControl2 和主窗口的代码。现在请让我知道其中的错误

【问题讨论】:

标签: wpf mvvm


【解决方案1】:

MainWindow.xaml

<Window x:Class="TestMultipleUserControl.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:controls="clr-namespace:TestMultipleUserControl" 
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <controls:UserControl1 Name="UserControl1Obj" Margin="10,-5,38,148"/>
        <controls:UserControl2 Name="UserControl2Obj" Margin="153,171,38,10"/>
    </Grid>
</Window>

MainWindow.xaml.cs

public partial class MainWindow : Window
{
    public delegate void ValuePassDelegate(int ValueToGet);
    public event ValuePassDelegate ValuePassEvent;

    public MainWindow()
    {
        InitializeComponent();
        ValuePassEvent += new ValuePassDelegate(method1);
        UserControl1Obj.del = ValuePassEvent;
    }
    public void method1(int ValueToGet)
    {
        UserControl2Obj.txtName.Text = ValueToGet.ToString();
    }
}

UserControl1.xaml

<UserControl x:Class="TestMultipleUserControl.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <Button Content="Button" HorizontalAlignment="Left" Margin="77,121,0,0" VerticalAlignment="Top" Width="166" Height="44" Click="Button_Click"/>

    </Grid>
</UserControl>

UserControl1.xaml.cs

public partial class UserControl1 : UserControl
{
    public Delegate del;
    public int ValueToPass = 0;
    public UserControl1()
    {
        InitializeComponent();
    }
    public void method1()
    {
        del.DynamicInvoke(ValueToPass);
    }
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        method1();
    }
}

UserControl2.xaml

<UserControl x:Class="TestMultipleUserControl.UserControl2"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" d:DesignWidth="300" Height="174.286">
    <Grid>
        <TextBox x:Name="txtName" HorizontalAlignment="Left" Height="46" Margin="51,55,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="211"/>

    </Grid>
</UserControl>

UserControl2.xaml.cs

public partial class UserControl2 : UserControl
{    
    public UserControl2()
    {
        InitializeComponent();    
    }
}

它会完美运行!!!

【讨论】:

    【解决方案2】:

    您需要使用数据绑定。

    上次(也是唯一一次)我这样做了,当控件 A 事件触发时,我从 xaml 更新了代码中的属性;然后,在属性更改事件处理程序上,更新绑定到 B 的属性,B 将自动更新。

    看看 MSDN 上的数据绑定。

    编辑:以防万一我不清楚,就我而言,我有一些逻辑要实现。如果您的情况是您实际上只是想在另一个控件上复制文本,那么您可以直接将文本字段绑定到同一个属性,它们都会一起更新。

    无论您找到什么样的最终解决方案,我都敢打赌,这将涉及数据绑定和一个或多个事件处理程序。

    【讨论】:

    • 就我而言,我必须在许多其他用户控件中使用用户控件 B。例如:像页眉/页脚。所以后面的代码可能不好?
    • 我们在这里讨论了多少个控件?一打?如果它足够小,它仍然是一样的。只需更新同一事件处理程序中涉及的所有属性。如果这足以让你不寒而栗,那么可能有一个更优雅的解决方案。但在这种情况下,您应该改写问题以明确该部分。
    【解决方案3】:

    您可以通过两种方式做到这一点。

    1. 正如 Ron Thompson 所说,通过数据绑定。

    2. 另一种方法是使用委托和事件

    通过委托将值从 UserControl1 传递给 ParentControl 事件,然后从父控件获取值到 UserControl2

    示例代码:

    public class UserControl1
    {
       public Delegate del;  
       public int ValueToPass = 0;
       public UserControl1() 
       {
           InitializeComponent();   
       }
       public void method1() 
       {
           del.DynamicInvoke(ValueToPass);
       }
    }
    
    public class ParentControl
    {
       public delegate void ValuePassDelegate(int ValueToGet);
       public event ValuePassDelegate ValuePassEvent;                 
    
       public UserControl1 UserControl1Obj = new UserControl1();
       public UserControl2 UserControl2Obj = new UserControl2();     
    
       public ParentControl() 
       {
           InitializeComponent();   
           ValuePassEvent += new ValuePassDelegate(method1);
           UserControl1Obj.del = ValuePassEvent;    
       }
       public void method1(int ValueToGet) 
       {
           UserControl2Obj.ValueToGet =  ValueToGet;
       }
    }
    
    public class UserControl2
    {   
       public int ValueToGet;
       public UserControl2()
       {
           InitializeComponent();
       }       
    }
    

    【讨论】:

    • 我在委托和 ValuePassDelegate 处遇到错误。
    • 编辑:错误的返回类型和委托关键字 public delegate void ValuePassDelegate(int ValueToGet);
    • @simran:k,很好。现在在 userControl1 中,我有一个按钮。在 userControl2 中,我有一个文本框。当我单击按钮时,应该在文本框中看到一个文本。为此,我能在这里做什么?
    • @B.K.N 我不是 simran :-) 我是 sriman .. 在 Usercontrol1 中,在 button1 单击事件中调用 method1() 在 ParentControl 的 method1() 中,设置 UserControl2 文本框的值。 UserControl2Obj.TextBox1.Text = ValueToGet
    • cool Sriman .....我收到一个错误“发生'System.NullReferenceException'类型的未处理异常”在del.DynamicInvoke(ValueToPass)行上;
    猜你喜欢
    • 1970-01-01
    • 2018-01-31
    • 2020-06-28
    • 2016-06-26
    • 2013-08-29
    • 2013-09-02
    • 2013-07-23
    • 1970-01-01
    • 2013-07-04
    相关资源
    最近更新 更多