2016-11-28 80 views
1

我經歷了類似的問題,但我沒有找到適合我的代碼的答案。我試圖從視圖模型稱爲客戶端視圖模型命名行(每個客戶端有多條線,所以我輸入的行應知道哪個客戶端是所有者)數據(客戶端類型Id)與WPF從一個視圖模型viewmodels之間通過屬性

更新:在AddCommand

這是客戶端代碼:

public class CRMClientViewModel : BaseViewModel 
{ 

    static string currentClient; 


    public Command AddLineCommand { get; set; } 

    public CRMClientViewModel(object obj) 
    { 

     AddLineCommand = new Command(OnAddLineCommand); 
    } 
    void OnAddLineCommand(object obj) 
    { 
     MainWindowViewModel.Instance.ShowCRMClient = false; 
     MainWindowViewModel.Instance.ShowCRMLine = true; 

    } 

    private string _clientId; 

    public string ClientId 
    { 
     get { return _clientId; } 
     set 
     { 
      _clientId = value; 
      RaisePropertyChanged(); 
      currentClient = value; 
     } 
    } 






} 
} 

基本視圖模型包含:

public class BaseViewModel : INotifyPropertyChanged 
{public event PropertyChangedEventHandler PropertyChanged; 
    protected void RaisePropertyChanged([CallerMemberName]string propertyName = null) 
    { 
     PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
    } 

} 

這是客戶視圖的XAML:

<UserControl x:Class="DiamondCellular.View.CRMClientView" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:ViewModel="clr-namespace:DiamondCellular.View.ViewModels" 
     xmlns:local="clr-namespace:DiamondCellular.View" 
     mc:Ignorable="d" 
     Height="350" Width="525" FontSize="18" Background="White"> 

    <UserControl.DataContext> 
     <ViewModel:CRMClientViewModel/> 
    </UserControl.DataContext> 




     <Button x:Name ="Add" Content="AddLine" Command ="{Binding AddLineCommand}" Grid.Column="4" Grid.Row="6" Foreground="#FFFF0012" Margin="0,10.4,17.8,9.8" VerticalAlignment="Center" HorizontalAlignment="Right" FontSize="12" Width="71"/> 



</UserControl> 

我想通過客戶端1d我從客戶端獲取視圖線路視圖模型

+0

的'CRMClientViewModel'維持基準(即屬性或ObservableCollection)到Line ViewModel? –

+0

「AddLineCommand」的代碼是什麼? – grek40

+0

Fer Garcia - 它們位於相同的命名空間中,如果您的意思是共同屬性,那麼它們只有共享的BaseViewModel – Ilanz

回答

0

如果我的理解是正確的,你想有一個經典的主詳細視圖又名親子。

您可以在構造函數中將「父」傳遞給您的「子」。

void OnAddLineCommand(object obj) 
{ 
//... 
var lineVM = new LineViewModel(this); 
this.Lines.Add(lineVM); 
//... 
} 

我假設你想使用客戶端的線(無論是什麼)。你需要持有的Line個物業

public ObservableCollection<LineViewModel>{get;} 
保存父時(在你的情況 Client)還保存所有的孩子

0

這就是我終於做到:

創建的repository類 中,我把singelton來初始化它只有一次,所有的視圖模型可以訪問 :

public class UIRepository 
{ 
    private static volatile UIRepository instance; 
    private static object syncRoot = new Object(); 

    private UIRepository(){} 
    public static UIRepository Instance 
    { 
     get 
     { 
      if (instance == null) 
      { 
       lock (syncRoot) 
       { 
        if (instance == null) 
         instance = new UIRepository(); 
       } 
      } 

      return instance; 
     } 
    } 

    private string _currentClientId; 

    public string CurrentClientId 
    { 
     get { return _currentClientId; } 
     set { _currentClientId = value; } 
    }} 

在客戶視圖模型類的屬性ClientID(具有約束力的XAML)我進入新的clientId

private string _clientId; 

    public string ClientId 
    { 
     get { return _clientId; } 
     set 
     { 
      _clientId = value; 
      RaisePropertyChanged(); 
      UIRepository.Instance.CurrentClientId = ClientId; 
     } 
    } 

釷恩我在當我更改爲新的視圖(通過使用綁定在visibility屬性進入ClientID的客戶端類增加了一個命令:

private void OnAddLineCommand(object obj) 
    { 
     MainWindowViewModel.Instance.ShowCRMClient = false; 
     MainWindowViewModel.Instance.ShowCRMLine = true; 

     UIRepository.Instance.CurrentClientId = ClientId; 

    } 

這就是所有