【问题标题】:Passing data between two usercontrols / Views在两个用户控件/视图之间传递数据
【发布时间】:2020-04-02 02:40:52
【问题描述】:

使用 MVVM

我正在尝试在一个视图 (view1) 中传递在控件(附加代码中的文本框)中输入的数据,并在第二个视图 (view2) 中使用该数据。目前,通过在 App.xaml 文件中声明我的所有视图,我可以将 view2 中的文本块与在 view1 的文本框中输入的信息绑定,并看到它显示在所述文本块中。但我也想使用在 view2 的视图模型中输入的信息,但不知道如何在那里访问它以使用这些信息。

有人可以告诉我如何去做吗?谢谢!

App.xaml [资源声明]

<Application.Resources>
<vws:DefaultVM x:Key="DefaultVMApp"></vws:DefaultVM>
<vws:View1 x:Key="View1App"></vws:View1>
<vws:View2 x:Key="View2App"></vws:View2>
<vm:AppVM x:Key="AppVMApp"></vm:AppVM>
<vm:View1VM x:Key="View1VMApp"></vm:View1VM>
<vm:View2VM x:Key="View2VMApp"></vm:View2VM>
</Application.Resources>

View1.xaml

    <UserControl.DataContext>
    <StaticResource ResourceKey="View1VMApp"></StaticResource>
    </UserControl.DataContext>
    <Grid Background="Aqua">
    <StackPanel Margin="100">
    <TextBox x:Name="firstNameTextBoxView1" Text="{Binding View1InfoClass.FirstName, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}"></TextBox>           
    <Button Command="{Binding Source={StaticResource AppVMApp}, Path=View2ButtonCommand}"  Content="Go to view2" Height="20" Width="70" />
    </StackPanel>
</Grid>

View2.xaml

<UserControl.DataContext>
        <StaticResource ResourceKey="View2VMApp"></StaticResource>
    </UserControl.DataContext>
    <Grid Background="Beige">
        <StackPanel Margin="100">

            <TextBlock x:Name="View1TextBlock" Text="{Binding Source={StaticResource View1VMApp}, Path=View1InfoClass.FirstName}" ></TextBlock>

        </StackPanel>
    </Grid>

AppVM

    public class AppVM : ObservableObject
        {
            //Create a property that controls current view
            private static object _currentView =  new DefaultVM();
            public object CurrentView
            {
                get { return _currentView; }
                private set
                {
                    OnPropertyChanged(ref _currentView, value);
                }
            }
            private string _textboxText;
            public string TextboxText
            {
                get { return _textboxText; }
                set
                {
                    OnPropertyChanged(ref _textboxText, value);
                }
            }
            public AppVM()
            {
                View1ButtonCommand = new RelayCommand(ShowView1, AlwaysTrueCommand);
                View2ButtonCommand = new RelayCommand(ShowView2, AlwaysTrueCommand);
                DefaultCommand = new RelayCommand(ShowDefault, AlwaysTrueCommand);  
            }
//Instantiate the relaycommands, we will need to instantiate relaycommand objects for every command we need to perform. 
            //This means that we will need to do this for preses of all buttons
            public RelayCommand View1ButtonCommand { get; private set; }
            public RelayCommand View2ButtonCommand { get; private set; }

            public RelayCommand DefaultCommand { get; private set; }


            public void ShowDefault(object dummy)
            {
                CurrentView = new DefaultVM();
            }

            public void ShowView1(object dummy)
            {
                CurrentView = new View1();
            }

            public void ShowView2(object dummy)
            {  
                CurrentView =  new View2();
            }

            public bool AlwaysTrueCommand(object dummy)
            {
                return true;
            }
        }

【问题讨论】:

    标签: c# wpf mvvm binding


    【解决方案1】:

    您的代码中的基本问题是您为每个用户控件专门提供了一个预定义的视图模型对象。这真的很糟糕。用户控件的数据上下文必须单独保留,以便客户端代码(例如您的主窗口)确定,并用于将用户控件公开的特定属性绑定到到。

    很遗憾,您的问题中没有足够的上下文来提供清晰、完整的答案。但要解决您的问题,您需要采取不同的做法:

    1. 首先,将用于用户控件的视图模型与用户控件本身“分离”。通过将依赖属性添加到每个用户控件,然后让使用用户控件的主视图决定绑定到每个依赖属性的内容来执行此操作。 不允许让用户控件自己设置自己的数据上下文。
    2. 完成此操作后,您可能会发现您可以对两个用户控件使用与主视图相同的视图模型。 IE。您将主视图的数据上下文设置为单视图模型,用户控件将继承该数据上下文,并且您将绑定,例如,TextboxText 属性到每个用户控件中适当声明的依赖项属性。这样,该单一属性将同时代表两个用户控件的状态。

    希望这足以让您重回正轨。如果没有,请考虑在 Stack Overflow 上搜索与视图模型及其与用户控件的关系相关的其他问题。比如这些问题:
    Issue with DependencyProperty binding
    XAML binding not working on dependency property?
    WPF DataBinding with MVVM and User Controls

    其他问题不能完全解决您的情况,但应该会给您一些关于构建视图模型的替代方法的想法:
    MVVM : Share data between ViewModels
    Sharing non control related data between WPF ViewModel and UserControl
    @987654326 @
    Sharing state between ViewModels

    【讨论】:

    • 您好彼得非常感谢您的回复。我不知道对用户控件上的 datcontext 进行硬绑定是一件坏事。我将浏览您指出的资源并重做此应用程序。太感谢了!如果我遇到任何问题会发布。
    猜你喜欢
    • 2012-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多