wpf CurrentViewModel属性更改未反映在ContentControl中

vlurs2pr  于 2022-11-18  发布在  其他
关注(0)|答案(2)|浏览(171)

你好,我到处都找过了,我不明白我做错了什么。我的命令被调用了,onpropertychanged事件被调用了,但是我的主窗口中的ContentControl不会显示下一个视图。我是WPF和Xaml的新手,我真的想弄清楚这一点。
我在Github上附加了一个示例应用程序的链接。
这里的结束游戏是我希望单选按钮被选中时,视图是活动的。我还希望能够改变从主视图到其他视图通过一个按钮上的视图,一旦我过去了这第一个冲刺。
Github repository

public class ApplicationViewModel : ObservableObject
    {
        private ICommand _changeViewCommand;

        private IViewModel _CurrentViewModel;
        private List<IViewModel> _viewModels;
        private bool _isActive;

        public ApplicationViewModel()
        {
            ViewModels.Add(new HomeViewModel());
            ViewModels.Add(new AccountViewModel());
            
            CurrentViewModel = ViewModels.Find(r => r.Name == "Home");
            CurrentViewModel.IsActive = true;
        }

        public List<IViewModel> ViewModels
        {
            get
            {
                if (_viewModels == null)
                    _viewModels = new List<IViewModel>();

                return _viewModels;
            }
        }

        public IViewModel CurrentViewModel
        {
            get
            {
                return _CurrentViewModel;
            }
            set
            {
                if (_CurrentViewModel != value)
                {
                    _CurrentViewModel = value;
                    OnPropertyChanged();
                }
            }
        }

        public ICommand ChangePageCommand
        {
            get
            {
                if (_changeViewCommand == null)
                {
                    _changeViewCommand = new RelayCommand(
                        p => ChangeViewModel((IViewModel)p),
                        p => p is IViewModel);
                }

                return _changeViewCommand;
            }
        }
       

        private void ChangeViewModel(IViewModel viewModel)
        {
            CurrentViewModel = ViewModels.Find(r => r.Name == viewModel.Name);
        }

        public bool IsActive
        {
            get
            {
                return _isActive;
            }
            set
            {
                _isActive = value;
                OnPropertyChanged("IsActive");
            }
        }
    }

    <UserControl.DataContext>
        <viewModels:ApplicationViewModel/>
    </UserControl.DataContext>
    <UserControl.Resources>
        <DataTemplate DataType="{x:Type viewModels:ApplicationViewModel}"/>
        <DataTemplate DataType="{x:Type viewModels:HomeViewModel}">
            <views:HomeView/>
        </DataTemplate>
        <DataTemplate DataType="{x:Type viewModels:AccountViewModel}">
            <views:AccountView/>
        </DataTemplate>
    </UserControl.Resources>
    <StackPanel Orientation="Vertical">
        <RadioButton Content="Home" IsChecked="{Binding IsActive, Mode=TwoWay}" Command="{Binding    DataContext.ChangePageCommand, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
                         CommandParameter="{Binding }">
            <RadioButton.DataContext>
                <viewModels:HomeViewModel/>
            </RadioButton.DataContext>
        </RadioButton>
        <RadioButton Content="Account" IsChecked="{Binding IsActive, Mode=TwoWay}" Command="{Binding DataContext.ChangePageCommand, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
                         CommandParameter="{Binding }">
            <RadioButton.DataContext>
                <viewModels:AccountViewModel/>
            </RadioButton.DataContext>
        </RadioButton>
    </StackPanel>
sshcrbum

sshcrbum1#

您应该将RadioButton连接到列表中的ViewModel。不要在xaml中创建新的视图模型。
和CurrentViewModel可以通过ContentControl与参考资料中声明的一个模板一起显示

<UserControl.DataContext>
    <viewModels:ApplicationViewModel/>
</UserControl.DataContext>
<UserControl.Resources>
    <DataTemplate DataType="{x:Type viewModels:HomeViewModel}">
        <views:HomeView/>
    </DataTemplate>
    <DataTemplate DataType="{x:Type viewModels:AccountViewModel}">
        <views:AccountView/>
    </DataTemplate>
</UserControl.Resources>

<StackPanel Orientation="Vertical">
    <ItemsControl ItemsSource="{Binding ViewModels}">
      <ItemsControl.ItemTemplate>
         <DataTemplate>
            <RadioButton Content="{Binding Name}" 
                 IsChecked="{Binding IsActive}" 
                 Command="{Binding DataContext.ChangePageCommand, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"
                 CommandParameter="{Binding }">
          </RadioButton>
        </DataTemplate>
      </ItemsControl.ItemTemplate>
    </ItemsControl>

    <ContentControl Content="{Binding CurrentViewModel}"/>
</StackPanel>

之后,您可能应该将StackPanel更改为Grid,以允许ContentControl占用所有可用的屏幕空间

nukf8bse

nukf8bse2#

看起来,如果我将AccountViewModel分配给按钮DataContext,并将RelativeSource AncestorType设置为x:Type Window,则HomeView上的按钮会将视图切换到AccountView。

<Button Height="40" Content="Account" Command="{Binding DataContext.ChangePageCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
                     CommandParameter="{Binding }">
        <Button.DataContext>
            <viewModels:AccountViewModel/>
        </Button.DataContext>
    </Button>

相关问题