如何在WPF MVVM项目中通过ViewModel共享数据?

问题描述 投票:0回答:1

我正在通过做一个简单的应用程序来学习 WPF,更一般地说是 MVVM。

我的主窗口包含 4 个视图。 一个用于主功能区,其中存储一些用户参数,一个用于设计表,其中具有可参数化的网格覆盖。

目标: 在 RibbonView 中,用户可以选择网格覆盖间距,我希望将整数数据发送到 RibbonViewModel,然后发送到 DesignSheetViewModel,然后用于刷新 DesignSheetView 网格覆盖。

RibbonView.xaml:(IntegerUpDown 的定义)

<UserControl x:Class="GrafcetDesignerWPF.Views.RibbonView"
         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" 
         xmlns:local="clr-namespace:GrafcetDesignerWPF.Views"
         xmlns:Fluent="clr-namespace:Fluent;assembly=Fluent" xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
         mc:Ignorable="d" 
         d:DesignHeight="450" d:DesignWidth="800">
<Grid>
    <Fluent:Ribbon Grid.Row="0">
        <Fluent:RibbonTabItem Header="View">
            <Fluent:RibbonGroupBox Header="Group">
                <xctk:IntegerUpDown 
Minimum="5" 
Maximum="50" 
Width="80" 
Increment="5" 
Value="10" />
            </Fluent:RibbonGroupBox>
        </Fluent:RibbonTabItem>
    </Fluent:Ribbon>
</Grid>

RibbonView.xaml.cs:(将视图绑定到其 ViewModel)

public partial class RibbonView : UserControl
{
    public RibbonView()
    {
        InitializeComponent();
        this.DataContext = new RibbonViewModel();
    }
}

DesignSheetView.xaml:(网格覆盖的定义)

<UserControl x:Class="GrafcetDesignerWPF.Views.DesignSheetView"
         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:controls="using:Microsoft.Toolkit.WinUI.UI.Controls"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:GrafcetDesignerWPF.Views"
         xmlns:converters="clr-namespace:GrafcetDesignerWPF.Converters"
         mc:Ignorable="d" 
         d:DesignHeight="450" d:DesignWidth="800"
         x:Name="DesignSheetControl">
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
    <Canvas x:Name="DesignCanvas" Width="2000" Height="2000">
        <Canvas.Background>
            <DrawingBrush TileMode="Tile" Viewport="0,0,20,20" ViewportUnits="Absolute">
                <DrawingBrush.Drawing>
                    <GeometryDrawing Brush="Transparent">
                        <GeometryDrawing.Pen>
                            <Pen Thickness="0.5" Brush="LightGray"/>
                        </GeometryDrawing.Pen>
                        <GeometryDrawing.Geometry>
                            <GeometryGroup>
                                <LineGeometry StartPoint="0,0" EndPoint="20,0"/>
                                <LineGeometry StartPoint="0,0" EndPoint="0,20"/>
                            </GeometryGroup>
                        </GeometryDrawing.Geometry>
                    </GeometryDrawing>
                </DrawingBrush.Drawing>
            </DrawingBrush>
        </Canvas.Background>
    </Canvas>
</ScrollViewer>

DesignSheetView.xaml.cs:

    public partial class DesignSheetView : UserControl
    {
        public DesignSheetView()
        {
            InitializeComponent();
            this.DataContext = new DesignSheetViewModel();
        }
    }

最后,我的 ViewModel 定义如下:

    internal class ViewModelBaseClass : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler? PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

internal class RibbonViewModel : ViewModelBaseClass
{

}
internal class DesignSheetViewModel : ViewModelBaseClass
{

}

我知道在DesignSheetView中,我需要用{Binding nameOfProperty}替换固定值。

以下是我的应用程序的概述: The setting of the grid spacing is in the top left corner, and the grid to be refreshed according this parameter is in the center of the app.

网格间距的设置在左上角,根据此参数刷新的网格在应用程序的中心。

c# wpf mvvm
1个回答
0
投票

我在 mvvmlight 中使用 Messenger。它发送消息
共享数据?我使用静态变量。

© www.soinside.com 2019 - 2024. All rights reserved.