【问题标题】:Prism 6.2 Binding to Model not ViewModelPrism 6.2 绑定到模型而不是 ViewModel
【发布时间】:2016-10-05 03:23:49
【问题描述】:

我正在尝试在 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 "";
                }
        }
    }
}

视图模型:

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>

【问题讨论】:

    标签: c# mvvm prism


    【解决方案1】:

    假设您已将 DataContext 正确设置为 MainDialogueViewModel 实例;您不需要在绑定中包含 MainDialogueViewModel。只需绑定到属性名称 XMLMasterFile。另请记住,如果值没有不同,则不会更新任何内容。

    【讨论】:

    • 当我单步调试调试器时,我可以看到 XMLMasterFile 值发生了变化,但视图没有更新。我在哪里可以查看 DataContext 是否设置正确?我认为如果没有使用 DataContext,xmlns:local 会将绑定指向正确的位置
    • 不,本地命名空间对 DataContext 没有任何作用。您必须先将 View 的 DataContext 设置为 ViewModel 的实例,然后才能使用任何绑定。
    • 感谢 Brian,我取出 MainDialogueViewModel,它现在可以工作了。
    猜你喜欢
    • 1970-01-01
    • 2011-07-19
    • 1970-01-01
    • 1970-01-01
    • 2015-07-04
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    相关资源
    最近更新 更多