为什么用户控件不从viewmodel绑定?

时间:2019-12-15 19:40:35

标签: c# .net wpf mvvm

我正在编写我的第一个“真实” MVVM应用程序,因此在这种情况下我需要帮助,因此我具有用户控件以及页面和主窗口,主窗口具有框架保持页面,用户控制进入页面,我在该页面上分配了相同的数据上下文Mainwindow和page,为进行用户控制,我放置了另一个datacontext但不起作用,我需要知道我想念的内容。

这是我的网页Xaml代码

    <Page x:Class="BudG.Pages.SreachForIdentity"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
  xmlns:local="clr-namespace:BudG.Pages"
   xmlns:UserControls="clr-namespace:BudG.UserControls"
  xmlns:ViewModel="clr-namespace:BudG.ViewsModel"
   Width="{Binding ActualWidth, 
          RelativeSource = {RelativeSource AncestorType = {x:Type Window}}}" 
  Height="{Binding ActualHeight, 
          RelativeSource ={RelativeSource AncestorType = {x:Type Window}}}"
  Title="SreachForIdentity" x:Name="SearchIdentity" Loaded="SearchIdentity_Loaded">
 <Grid x:Name="LayoutRoot">
    <UserControls:SearchForIdentityView DataContext="{Binding LogInViewModel}"/>
</Grid>
</Page>

这是我的SearchForIdentityView“ UserControl” Xaml代码

<UserControl x:Class="BudG.UserControls.SearchForIdentityView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
         xmlns:local="clr-namespace:BudG.UserControls">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="10"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="5"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="10"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="5"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <TextBlock x:Name="LblTitle" Grid.Row="1" Grid.Column="1"  Text="Find your account."   Padding="10"  Height="40"  HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="16" Foreground="#FF747070"   FontFamily="Times New Roman" />
    <xctk:WatermarkTextBox x:Name="TxtNationalID" Grid.Row="3" Grid.Column="1" Width="300" Watermark="Enter new National Id" Padding="8" Style="{DynamicResource WatermarkTextBoxStyle1 }" Cursor="IBeam" />
    <Button Height="40" Content="Search" Grid.Row="7" Grid.Column="1" Width="150" Style="{DynamicResource StyleOfSaveButton}" VerticalAlignment="Top" Margin="89.5,0,89.5,0" Cursor="Hand" Command="{Binding LogInCommand}"/>
</Grid>
</UserControl>

这是背后的UserControl代码

 public partial class SreachForIdentityView : UserControl
{
    public SreachForIdentityView()
    {
        InitializeComponent();

    }

}

和loginviewmodel代码

 public class LogInViewModel : ViewModelBase, ILogInViewModel
{
    private IEventAggregator _eventAggregator;
    private IUserRepository _userRepository;
    public LogInViewModel(IUserRepository userRepository, IEventAggregator eventAggregator)
    {
        _userRepository = userRepository;
        _eventAggregator = eventAggregator;
LogInCommand=new DelegateCommand(OnLogInExecute);
    }

private void OnLogInExecute()
{
Messagebox.Show("done");
}
    public async Task LoadAsyncByPassword(string UserName, string password)
    {
        var user = await _userRepository.GetAsyncByPassword(UserName, password);
        if(user==null)
        {
            _eventAggregator.GetEvent<OpenDetailViewEvent>().Publish(0);
        }
        else
        { _eventAggregator.GetEvent<OpenDetailViewEvent>().Publish(user.IdAccount); }

    }

public ICommand LogInCommand{get;set;}
}

这是MainViewModel代码

 public IEventAggregator _eventaggregator;
    private Func<IUserAccountViewModel> _useraccountviewmodelCreator;
    private IUserAccountViewModel _useraccountViewModel;
    private ILogInViewModel _LoginViewModel;
    private int _checkid;
    private Func<ILogInViewModel> _loginViewModelCreator;
    private Page _PageToNavigate;
    private Func<INotificationViewModel> _notificationViewModel;
    public MainViewModel(INavigationViewModel navigationveiwmodel,
        Func<IUserAccountViewModel> useraccountviewmodelCreator,
       Func<ILogInViewModel> loginViewModelCreator, Func<INotificationViewModel> notificationViewModel
        , IEventAggregator EventAggregator)
    {
        _useraccountviewmodelCreator = useraccountviewmodelCreator;
        _loginViewModelCreator = loginViewModelCreator;
        _eventaggregator = EventAggregator;
        _notificationViewModel = notificationViewModel;
        _eventaggregator.GetEvent<OpenDetailViewEvent>()
            .Subscribe(OnOpenDetailView);
        _eventaggregator.GetEvent<AfterUserSavedEvent>()
            .Subscribe(AfterUserSaved);
        NavigationViewModel = navigationveiwmodel;
        CreateNewUserCommand = new DelegateCommand(OnCreateNewUserExecute);
        ForgetPassword = new DelegateCommand(OnForgetPassword);

    }

    private void OnForgetPassword()
    {
        PageToNavigate = new Pages.SreachForIdentity(this);
    }



    public INavigationViewModel NavigationViewModel { get; set; }
    public ICommand CreateNewUserCommand { get; set; }
    public ICommand ForgetPassword { get; set; }
    public string UserName { get; set; }
    public string Password { get; set; }
    public bool Result { get; set; }


    public Page PageToNavigate
    {
        get { return _PageToNavigate; }
        set { _PageToNavigate = value; OnPropertyChanged(); }
    }


    public int CheckID
    {
        get { return _checkid; }
        set { _checkid = value; OnPropertyChanged(); }
    }



    public ILogInViewModel LogInViewModel
    {
        get { return _LoginViewModel; }
        set { _LoginViewModel = value; OnPropertyChanged(); }
    }

    public IUserAccountViewModel UserAccountViewModel
    {
        get { return _useraccountViewModel; }
        set
        {
            _useraccountViewModel = value;
            OnPropertyChanged();
        }
    }

    private INotificationViewModel _NotificationViewModel;

    public INotificationViewModel NotificationViewModel
    {
        get { return _NotificationViewModel; }
        set { _NotificationViewModel = value; OnPropertyChanged(); }
    }

}

}

所以按钮中的命令根本不起作用,我也不知道为什么?

1 个答案:

答案 0 :(得分:1)

答案是我必须为LogInViewModel属性分配一个实例,以使其工作

LogInViewModel = _viewmodel._loginViewModelCreator();

LogInViewModel = new LogInViewModel();

感谢@Clemens帮助我,并指导我回答