2010-11-02 32 views
2

比方说,我有一个用户控件test1.xaml,并有一个框架控件名称frame1。在我的第二个用户控件上,如何引用test2.xaml中的test1.xaml以便操作test2.xaml.cs中的控件属性?因为我知道test1 test = new test1();将不会工作,因为我没有实例化它,并没有引用它。我可以问一下吗?如何从另一个类访问控件?

回答

1

好的。我不会用DependencyProperties编写代码,因为它有异味。我会写一个简单的代码,使用MVVM来做这样的事情。但是我想指出的是,您必须阅读Josh Smith撰写的一篇文章“使用Model-View-ViewModel设计模式的WPF应用程序”。 这是一个简单的代码,其中包含一个主窗口和两个用户控件Test1和Test2。并且只有一个ViewModel - GodViewModel,它是Test1和Test2的viewModel。事实上,通常在ViewModel和View之间有1-1映射。为了简单起见,我只创建了一个ViewModel。

窗口代码:

<Window x:Class="WpfApplication99.MainWindow" 
     x:Name="GodWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:view="clr-namespace:WpfApplication99.View" 
     Title="MainWindow" 
     xmlns:local="clr-namespace:WpfApplication99" 
     DataContext="{Binding Vm, ElementName=GodWindow}"> 
    <StackPanel> 
     <view:Test1 /> 
     <view:Test2 /> 
    </StackPanel> 
</Window> 

public partial class MainWindow : Window 
{ 
    ViewModel.GodViewModel _vm = new ViewModel.GodViewModel(); 

    public ViewModel.GodViewModel Vm 
    { 
     get { return _vm; } 
     set { _vm = value; } 
    } 

    public MainWindow() 
    { 
     InitializeComponent(); 
    } 
} 

视图模型的代码:

namespace WpfApplication99.ViewModel 
{ 
    public class GodViewModel 
    { 
     public string Text { get; set; } 
    } 
} 

TEST1代码(代码后面是空的):

<UserControl x:Class="WpfApplication99.View.Test1" 
      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="45" d:DesignWidth="167"> 

     <Button Content="{Binding Text}" 
       Height="26" 
       Name="button1" 
       Width="144" /> 
</UserControl> 

TEST2代码(代码后面是空的) :

<UserControl x:Class="WpfApplication99.View.Test2" 
      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" > 
     <TextBox Text="{Binding Text}" Height="69" Width="232" /> 
</UserControl> 

使用此代码,您可以在Test1和Test2中使用相同的属性Text。在你的问题中,你写道你在test1中有一个属性,并且想在test2中使用它。所以想象一下,提供的解决方案只是将test1的一个属性Text放入GodViewModel中。 也许,你想在后面的代码中使用它。在这种情况下,您应该为test1和test2用户控件创建单独的ViewModel。我无法描述那里的所有细节。所以,请阅读文章。我确信MVVM模式是WPF中的关键。

+0

感谢您的时间和源代码!我赞赏它 – 2010-11-03 17:13:51

0

您可以将YourViewModelBase类型的DependencyProperty添加到您的test2。在你创建控件实例的地方写一些东西。当然,如果你使用MVVM。不过,据我了解,根据MVVM你不应该这样做。 如果你没有YourViewModelBase,你可以为你的test1创建一个具有必要属性的抽象类,或者只是将test1作为UserControl传递,然后尝试将它转换到test2代码中的test1。

+0

嗨,谢谢你的回复。你能否提供一个示例代码来说明如何实现这个 – 2010-11-02 16:15:46

2

在MVVM方法中,视图/用户控件都可以使用与数据绑定的相同视图模型。现在,当第一个控件引起该视图模型公开的其中一个属性发生值更改时,它会自动反映到第二个用户控件中。

+0

我不确定我是否已经使用MVVM,但是我已经根据它们的文件夹分离了这些文件。我试图实现MVVM。 – 2010-11-02 16:16:09