Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在ViewModel构造函数之外更改时,ContentControl视图不更新_C#_.net_Wpf_Mvvm_Data Binding - Fatal编程技术网

C# 在ViewModel构造函数之外更改时,ContentControl视图不更新

C# 在ViewModel构造函数之外更改时,ContentControl视图不更新,c#,.net,wpf,mvvm,data-binding,C#,.net,Wpf,Mvvm,Data Binding,当我在构造函数中设置ContentControl时,MainMenuView中的ContentControl将更新,但之后不会更新。我正试图让它改变按钮点击。该命令似乎正在工作并更改my CurrentPageViewModel的值。下面是更改视图的代码示例 MainMenuViewModel: public MainMenuViewModel() { SelectViewCommand = new RelayCommand<IPageViewModel>(s

当我在构造函数中设置ContentControl时,MainMenuView中的ContentControl将更新,但之后不会更新。我正试图让它改变按钮点击。该命令似乎正在工作并更改my CurrentPageViewModel的值。下面是更改视图的代码示例

MainMenuViewModel:

public MainMenuViewModel()
    {
        SelectViewCommand = new RelayCommand<IPageViewModel>(s => ChangeViewModel(s));
        PageViewModels.Add(new ClockViewModel());

        CurrentPageViewModel = PageViewModels[1];
    }
    private List<IPageViewModel> pageViewModels;
    public List<IPageViewModel> PageViewModels
    {
        get
        {
            if (pageViewModels == null)
                pageViewModels = new List<IPageViewModel>();

            return pageViewModels;
        }
    }

    private IPageViewModel currentPageViewModel;
    public IPageViewModel CurrentPageViewModel
    {
        get { return currentPageViewModel; }
        set { currentPageViewModel = value; OnPropertyChanged("currentPageViewModel"); }
    }

    public RelayCommand<IPageViewModel> SelectViewCommand { get; private set; }

    private void ChangeViewModel(IPageViewModel viewModel)
    {
        if (!PageViewModels.Contains(viewModel))
            PageViewModels.Add(viewModel);

        CurrentPageViewModel = PageViewModels
            .FirstOrDefault(vm => vm == viewModel);
    }

    private void OnPropertyChanged(string propertyName)
    {
        if (string.IsNullOrEmpty(propertyName))
            throw new ArgumentNullException("propertyName");

        var changed = PropertyChanged;
        if (changed != null)
        {
            changed(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
public主菜单视图模型()
{
选择ViewCommand=new RelayCommand=>ChangeViewModel;
添加(新的ClockViewModel());
CurrentPageViewModel=PageViewModels[1];
}
私有列表页面视图模型;
公共列表页面视图模型
{
得到
{
如果(pageViewModels==null)
pageViewModels=新列表();
返回页面视图模型;
}
}
私有IPageViewModel currentPageViewModel;
公共IPageViewModel CurrentPageViewModel
{
获取{return currentPageViewModel;}
设置{currentPageViewModel=value;OnPropertyChanged(“currentPageViewModel”);}
}
public RelayCommand selectview命令{get;private set;}
私有void ChangeViewModel(IPageViewModel viewModel)
{
如果(!PageViewModels.Contains(viewModel))
PageViewModels.Add(viewModel);
CurrentPageViewModel=PageViewModels
.FirstOrDefault(vm=>vm==viewModel);
}
私有void OnPropertyChanged(字符串propertyName)
{
if(string.IsNullOrEmpty(propertyName))
抛出新ArgumentNullException(“propertyName”);
var changed=Property changed;
如果(已更改!=null)
{
更改(此,新PropertyChangedEventArgs(propertyName));
}
}
公共事件属性更改事件处理程序属性更改;
MainMenuView:

<Window x:Class="TextApp.View.MainMenuView"
    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:local="clr-namespace:TextApp.View"
    xmlns:local1="clr-namespace:TextApp.ViewModel"
    mc:Ignorable="d"
    Title="MainMenuView" Height="450" Width="800">
<Window.Resources>
    <DataTemplate DataType="{x:Type local1:ClockViewModel}">
        <local:ClockView/>
    </DataTemplate>
    <DataTemplate DataType="{x:Type local1:IPageViewModel}">
        <local:IPageView/>
    </DataTemplate>
</Window.Resources>
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="0.3*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <StackPanel Grid.Column="0">
        <ItemsControl ItemsSource="{Binding PageViewModels}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Button Content="{Binding Name}"
                            Command="{Binding DataContext.SelectViewCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
                            CommandParameter="{Binding}"
                            Margin="2,5"/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </StackPanel>
    <ContentControl Grid.Column="1" Content="{Binding CurrentPageViewModel}"/> 
</Grid>


CurrentPageViewModel
属性的设置器中,您正在通知支持字段
CurrentPageViewModel
,而不是属性。我会尝试更新它,看看它是否解决了你的问题。嘿,我刚刚试过。它不起作用。您的视图模型未实现InotifyPropertyChanged。我创建了一个示例应用程序,它似乎对我有效,但我填充了您提供的代码中没有的部分。你能用现在的全视图模型类更新你的问题吗?嘿,我忘记回答了。我忘记从InotifyPropertyChanged实现。它现在正在工作。