我經歷了類似的問題,但我沒有找到適合我的代碼的答案。我試圖從視圖模型稱爲客戶端視圖模型命名行(每個客戶端有多條線,所以我輸入的行應知道哪個客戶端是所有者)數據(客戶端類型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我從客戶端獲取視圖線路視圖模型
的'CRMClientViewModel'維持基準(即屬性或ObservableCollection)到Line ViewModel? –
「AddLineCommand」的代碼是什麼? – grek40
Fer Garcia - 它們位於相同的命名空間中,如果您的意思是共同屬性,那麼它們只有共享的BaseViewModel – Ilanz