2016-10-05 99 views
0

我想在C#中使用Prism,但我似乎已經設置它,以便它綁定到我的模型中的項目,而不是我的viewmodel。這是一個簡短的程序,更多的是一個學習工具,任何東西。當我將項目移動到ViewModel時,SetProperty似乎沒有通知更改的視圖。棱鏡6.2綁定到模型不ViewModel

有關我如何反向安裝的任何想法?

型號:

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> 

回答

1

假設你有正確的DataContext的設置,以MainDialogueViewModel的實例;你不需要在你的綁定中包含MainDialogueViewModel。只需綁定到屬性名稱XMLMasterFile。另外請記住,如果該值沒有不同,則不會更新。

+0

當我通過調試器時,我可以看到XMLMasterFile值更改,但未更新視圖。我會在哪裏看到DataContext是否正確設置?如果沒有使用DataContext,我認爲xmlns:local會將綁定指向正確的位置 – coolercargo

+0

不,本地名稱空間對DataContext不起作用。您必須先將視圖的DataContext設置爲ViewModel的實例,然後才能使用任何綁定。 –

+0

感謝Brian,我拿出了MainDialogueViewModel,它現在正在工作。 – coolercargo