Prism 6.2绑定到Model而不是ViewModel

本文关键字:ViewModel Model 绑定 Prism | 更新日期: 2023-09-27 18:11:37

我试图在c#中使用Prism,但我似乎已经设置了它,以便它绑定到我的模型而不是我的视图模型中的项目。这是一个很短的项目,更像是一个学习工具。当我将项目移动到Viewmodel时,SetProperty似乎没有通知视图更改。

关于我是如何反向设置的,有什么想法吗?

模型:

namespace XMLValueModifier_Threaded_WPF.Models
{
    public class XMLReadFileModel : BindableBase
    {
        private string _XMLMasterFile2 = "0";
        public string XMLGetFileName()
        {
            if (_XMLMasterFile2 != "1")
                {
                    Microsoft.Win32.OpenFileDialog _XMLMasterFileDialog = new Microsoft.Win32.OpenFileDialog();
                    _XMLMasterFileDialog.DefaultExt = "xml";
                    _XMLMasterFileDialog.Filter = "xml Files (*.xml; *.XML) | *.xml; *.XML";
                    Nullable<bool> result = _XMLMasterFileDialog.ShowDialog();
                    if (result == true)
                    {
                        return _XMLMasterFileDialog.FileName;
                    }
                    return "";
                }
                else
                {
                    return "";
                }
        }
    }
}

ViewModel:

namespace XMLValueModifier_Threaded_WPF.ViewModels
{
    public class MainDialogueViewModel : BindableBase
    {
        private string _XMLMasterFile;
        public ICommand MasterFileLocation
        {
            get;
            set; 
        }
        public ICommand XMLFileLocation
        {
            get;
            set;
        }
        public string XMLMasterFile
        {
            get
            {
                return _XMLMasterFile;
            }
            set
            {
                SetProperty(ref _XMLMasterFile, value);
            }
        }
        private XMLReadFileModel xmlReadFileModel = new XMLReadFileModel();
        public MainDialogueViewModel()
        {
            XMLReadFileModel xmlReadFileModel = new XMLReadFileModel();
            Message = "example message";
            XMLMasterFile = "example File";
            this.MasterFileLocation = new DelegateCommand(chooseFile, canChooseFile);
            this.XMLFileLocation = new DelegateCommand(chooseFile, canChooseFile);
        }
        public void masterfilelocation()
        {
            MessageBox.Show("i am here");
            return;
        }
        private void chooseFile()
        {
           XMLMasterFile = xmlReadFileModel.XMLGetFileName();
        }
        private bool canChooseFile()
        {
            if (XMLMasterFile != null)
            {
                return true;
            }
            else
            {
                return true;
            }
        }
    }
}
XAML:

<Window x:Class="XMLValueModifier_Threaded_WPF.Views.MainDialogue"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:XMLValueModifier_Threaded_WPF.ViewModels" Width="625" Height="452"
    >
<Grid Margin="0,-24,0,-3">
    <TextBox x:Name="Textbox1" Text="{Binding MainDialogueViewModel.XMLMasterFile,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Height="23" Margin="25,120,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="425"/>
    <TextBox x:Name="Textbox2" Text="{Binding MainDialogueViewModel.XMLFiles,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Height="23" Margin="25,188,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="425" RenderTransformOrigin="0.506,1.565"/>
    <Label Content="Location of Master XML File" HorizontalAlignment="Left" Margin="25,89,0,0" VerticalAlignment="Top"/>
    <Label Content="Location of XML File(s)" HorizontalAlignment="Left" Margin="25,157,0,0" VerticalAlignment="Top"/></GRID>

Prism 6.2绑定到Model而不是ViewModel

假设你有你的DataContext设置正确的maindialgueviewmodel实例;你不需要在绑定中包含maindialgueviewmodel。只需绑定到属性名XMLMasterFile。还请记住,如果值不不同,则不会更新任何内容。