c# - 在两个用户控件/ View 之间传递数据

标签 c# wpf mvvm binding

使用MVVM

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

有人可以告诉我该怎么做吗?谢谢!

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;
            }
        }

View1 and View2 layout, kind of TLDR

最佳答案

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

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

  1. 首先,也是最重要的,将用于用户控件的 View 模型与用户控件本身“分离”。为此,请向每个用户控件添加依赖项属性,然后让使用用户控件的主视图决定将哪些内容绑定(bind)到每个依赖项属性。不允许用户控件自己设置自己的数据上下文。
  2. 完成此操作后,您可能会发现可以对两个用户控件使用与主视图相同的 View 模型。 IE。您将主视图的数据上下文设置为单一 View 模型,用户控件将继承该数据上下文,并且您将例如将 TextboxText 属性绑定(bind)到每个用户控制。这样,该单个属性将同时表示两个用户控件的状态。

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

其他问题不能完全解决您的场景,但应该为您提供一些构建 View 模型的替代方法的想法:
MVVM : Share data between ViewModels
Sharing non control related data between WPF ViewModel and UserControl
Sharing data between different ViewModels
Sharing state between ViewModels

关于c# - 在两个用户控件/ View 之间传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60983439/

相关文章:

c# - ASP.NET 捕获并替换 Global.asax 中的输出

java - 具有泛型类型的 ViewModel

c# - 设置外部应用程序焦点

c# - winXP上的https请求错误

c# - YamDocument 以 3 个点结尾的文本表示

c# - 用于加载 RTMP 源的应用程序的视频播放器

c# - 包含永无止境任务的对象会被垃圾回收吗?

c# - 用户控件的子依赖对象的绑定(bind)不起作用

c# - 如何动态更改 wpf MVVM light 中用户控件中存在的按钮(单击)上的用户控件

wpf - Fluent Ribbon SelectedTabItem 或 SelectedTabIndex