更改ViewModel对象不会反映对View的更改

时间:2016-09-01 12:01:07

标签: c# wpf xaml mvvm

我已经实现了一个用户控件,我已经将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;
}

1 个答案:

答案 0 :(得分:1)

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

因为我像这样绑定了用户控件......

this.DataContext = this;

我必须在User Control类本身上实现INotifyPropertyChange并在PropertyChanged属性上实现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));
}

但是我没有完全更新视图,因为我使用集合在TabControl中生成标签。

我必须在选择TabComtrol.Items的选择更改事件时使用此选项刷新ComboBox的视图,如下所示:

tcOrientation.Items.Refresh();

这解决了我的问题:)

相关问题