更改ViewModel对象并不反映对视图的更改

本文关键字:视图 ViewModel 对象 更改 | 更新日期: 2023-09-27 18:05:04

我已经实现了一个用户控制,我已经将DataContext绑定到自己,像这样…

this.DataContext = this;

在用户控件顶部有一个组合框。在它的选择改变事件上。我正在更新UserControl的属性,这应该导致整个视图重新生成,包括TabControlTextBoxes中的一些选项卡。

在选择更改事件中,我像这样更新属性…

this.CurrentViewModel = viewModel;

下面是视图中的一些示例xaml。

<TextBox 
    x:Name="txtPageSetupAlias" 
    Width="500" 
    Padding="2,5,0,0" 
    Text="{Binding Path=CurrentViewModel.ValidPageSetupAlias, Mode=TwoWay}" 
    Style="{StaticResource ResourceKey=txtAlias}" DockPanel.Dock="Left" 
    />
<TabControl 
    x:Name="tcOrientation" 
    Grid.Row="1" 
    BorderBrush="Transparent" 
    BorderThickness="0"
    Margin="0,0,0,0" 
    TabStripPlacement="Top" 
    ItemsSource="{Binding Path=CurrentViewModel.Orientations}">

和TabControl…

<TabControl.ContentTemplate>
    <DataTemplate>
        <Grid>
            <StackPanel 
                Orientation="Horizontal" 
                Margin="0,0,0,5">
                <TextBlock 
                    Text="Page Width:" 
                    Style="{StaticResource tbDataLabel}" 
                    Margin="0,0,34,0"
                    />
                <Border 
                    Width="98" 
                    CornerRadius="5" 
                    BorderBrush="#CCCCCC" 
                    Background="White" 
                    BorderThickness="1" 
                    Margin="7,0,0,0" 
                    >
                    <xctk:DecimalUpDown 
                        x:Name="pageDimWidth" 
                        Value="{Binding Path=PageWidth, Mode=TwoWay, ValidatesOnDataErrors=True, NotifyOnValidationError=True}" 
                        Validation.Error="page_Error" Increment="0.10"  
                        ParsingNumberStyle="Float" 
                        Style="{StaticResource dudValues}"
                        d:DataContext="{d:DesignInstance dtOs:PageSizeOrientationViewModel}" 
                        />
更新1:

这里是CurrentViewModel属性

private PageSetupEditorViewModel _currentViewModel;
public PageSetupEditorViewModel CurrentViewModel
{
    get { return _currentViewModel; }
    set
    {
        _currentViewModel = value;
        OnPropertyChanged(nameof(CurrentViewModel));
    }
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

谁能告诉我我在这里错过了什么?

更新2:

共享组合选择更改事件:

private void cmbPageSetupTemplate_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    _errors = 0;
    var validPageSetup = cmbPageSetupTemplate.SelectedItem as PageSetupEditorViewModel;
    CurrentViewModel = viewModel;
    this.DataContext = this;
}

更改ViewModel对象并不反映对视图的更改

显然,我必须做两件事才能使它工作。

由于我像这样将User Control绑定到它自己…

this.DataContext = this;

我必须在用户控制类本身上实现INotifyPropertyChange,并在CurrentViewModel属性上实现PropertyChanged调用,如下所示:

private PageSetupEditorViewModel _currentViewModel;
public PageSetupEditorViewModel CurrentViewModel
{
    get { return _currentViewModel; }
    set
    {
        _currentViewModel = value;
        OnPropertyChanged(nameof(CurrentViewModel));
    }
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

但这并没有完全更新视图,因为我正在使用一个集合来生成TabControl中的选项卡。

我必须在ComboBox的选择更改事件的选择上使用这个来刷新TabComtrol.Items的视图,如下所示:

tcOrientation.Items.Refresh();

这个解决了我的问题:)