簡體   English   中英

在兩個用戶控件/視圖之間傳遞數據

[英]Passing data between two usercontrols / Views

使用 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>

查看1.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>

視圖2.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>

應用虛擬機

    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 和 View2 布局,一種 TLDR

您代碼中的根本問題是您為每個用戶控件都指定了一個預定義的視圖模型對象。 真的很糟糕。 用戶控件的數據上下文必須單獨保留,以便客戶端代碼(例如您的主窗口)確定並用於綁定用戶控件公開的特定屬性。

不幸的是,您的問題中沒有足夠的上下文來提供清晰、完整的答案。 但是要解決您的問題,您需要以不同的方式做事:

  1. 首先,最重要的是,將您用於用戶控件的視圖模型與用戶控件本身“分離”。 通過向每個用戶控件添加依賴項屬性,然后讓使用用戶控件的主視圖決定將哪些內容綁定到每個依賴項屬性來完成此操作。 不要讓用戶控制自己設置自己的數據環境。
  2. 完成后,您可能會發現您可以對兩個用戶控件使用與主視圖相同的視圖模型。 即,您將主視圖的數據上下文設置為單個視圖模型,用戶控件將繼承該數據上下文,並且您將例如將TextboxText屬性綁定到每個用戶控件中適當的聲明依賴項屬性。 這樣,該單個屬性將同時表示兩個用戶控件的狀態。

人們希望這足以讓你回到正軌。 如果沒有,請考慮在 Stack Overflow 上搜索與視圖模型及其與用戶控件的關系相關的其他問題。 例如,這些問題:
DependencyProperty 綁定問題
XAML 綁定對依賴屬性不起作用?
帶有 MVVM 和用戶控件的 WPF 數據綁定

其他問題不能完全解決您的情況,但應該為您提供一些關於構建視圖模型的替代方法的想法:
MVVM:在 ViewModel 之間共享數據
在 WPF ViewModel 和 UserControl 之間共享非控件相關數據
在不同的 ViewModel 之間共享數據
在 ViewModel 之間共享狀態

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM