wpf 当特定状态改变时更新TabControl ItemSource ViewModel

8gsdolmq  于 2022-11-18  发布在  其他
关注(0)|答案(1)|浏览(272)

我有一个绑定到视图模型TabViewModelsCollectionObservableCollection属性的TabControl
当从视图模型DeviceState中设置另一个属性时,我想引发一个属性更改事件,并告诉我的TabControlItemSource进行刷新。
问题是我的ItemSourceViewModelsObservableCollection,当我调用RaisePropertyChanged("TabViewModelsCollection");时,没有任何东西得到更新。
此外,我的选项卡视图包含多个用户控件和绑定。
设想的场景是:设备位于网络上,收集数据,然后更新设备信息选项卡。
目前我的TabControl只在我选择一个不同的设备,然后选择我想看的设备whos信息时更新。想想左面板的设备列表,右面板的设备信息标签。
让我知道你们可能想看的代码的哪一部分,我的代码库很大,所以很难发布它。
下面是我的视图中TabControl的定义位置:

<!-- Devist List Controls -->
        <Grid DockPanel.Dock="Left" Margin="5,5">
            <local:DeviceListView DataContext="{Binding DeviceListViewModel}" Grid.Row="0"/>
        </Grid>
        <GroupBox Header="Device Information" DockPanel.Dock="Right" Margin="0,0,5,5">
            <TabControl IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding DeviceListViewModel.SelectedDevice.TabViewModelsCollection}" SelectedItem="{Binding DeviceListViewModel.SelectedDevice.SelectedTabItemVm}"   >
                <TabControl.Resources>
                    <DataTemplate DataType="{x:Type vms:HomeViewModel}">
                        <local:HomeTab/>
                    </DataTemplate>
                    <DataTemplate DataType="{x:Type vms:ConfigurationViewModel}">
                        <Grid>
                            <local:ConfigurationFileView  Visibility="{Binding Configuration, TargetNullValue=Collapsed, FallbackValue=Visible}"/>
                            <local:ErrorTab  Visibility="{Binding Path= Configuration, TargetNullValue=Visible, FallbackValue=Hidden}"/>
                        </Grid>
                    </DataTemplate>
                    <DataTemplate DataType="{x:Type vms:ExpansionModulesViewModelFactory}">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="*"/>
                                <RowDefinition Height="35"/>
                            </Grid.RowDefinitions>
                            <StackPanel Grid.Row="0">
                                <DockPanel >
                                    <local:ExpansionModulesList Title="Discovered/Enumerated" 
                                                        DataContext="{Binding DiscoveredModules}" 
                                                       />
                                    <GridSplitter Width="5"/>
                                    <local:ExpansionModulesList Title="User Action Required" 
                                                        DataContext="{Binding FaultyModules}" 
                                                        />
                                </DockPanel>
                            </StackPanel>
                            <DockPanel Grid.Row="1">
                                    <StackPanel Orientation="Horizontal" FlowDirection="RightToLeft" Margin="5" IsEnabled="{Binding IsCommandEnabled}">
                                        <Button Content="Cancel" HorizontalAlignment="Right"             
                                             Command="{Binding CancelExpansionCommand }"
                                             ToolTip="Revert all local modifications by refreshing data from the controller." />                                                                                                                                  
                                        <Separator Width="10"/>

                                        <Button Content="Apply"  HorizontalAlignment="Center"                         
                                             Command="{Binding ApplyExpansionCommand }"
                                             ToolTip="Apply all changes to the controller." />
                                        <Separator/>
                                    </StackPanel>
                                </DockPanel>

                        </Grid>
                    </DataTemplate>
                    <DataTemplate DataType="{x:Type vms:LogViewModel}">
                        <local:LogView  />
                    </DataTemplate>
                    <DataTemplate DataType="{x:Type vms:SignalStrengthViewModel}">
                        <local:SignalStrengthView  />
                    </DataTemplate>
                </TabControl.Resources>

                <TabControl.ItemContainerStyle>

                    <Style TargetType="{x:Type TabItem}">
                        <Setter Property="Header" Value="{Binding Name}" />
                        <Setter Property="IsEnabled" Value="{Binding IsEnabled}" />
                        <Setter Property="Header" Value="{Binding Name}" />
                    </Style>
                </TabControl.ItemContainerStyle>

编辑:我希望能够调用在此更改的引发属性,并让它刷新我所有的选项卡视图。

<TabControl IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding DeviceListViewModel.SelectedDevice.TabViewModelsCollection}" SelectedItem="{Binding DeviceListViewModel.SelectedDevice.SelectedTabItemVm}"   >

RaisePropertyChanged("TabViewModelsCollection");

fcg9iug3

fcg9iug31#

嗨,请尝试下一个解决方案:

VM代码修订

private State _deviceState;
    private ObservableCollection<object> _tabViewModelsCollection;
    public State DeviceState
    {
        get { return _deviceState; }
        set
        {
            _deviceState = value;
            RaisePropertyChanged("DeviceState");
            UpdateTabViewModelsCollection();
        }
    }

    public ObservableCollection<object> TabViewModelsCollection
    {
        get
        {
            return _tabViewModelsCollection ??
                   (_tabViewModelsCollection = new ObservableCollection<object>(GetDeviceData()));
        }
    }

    private void UpdateTabViewModelsCollection()
    {
        _tabViewModelsCollection = null;
        RaisePropertyChanged("TabViewModelsCollection");
    }

    private List<object> GetDeviceData()
    {
        //implement here the data collection process
        throw new NotImplementedException();
    }

XAML密文(定义UpdateSourceTrigger)

ItemsSource="{Binding DeviceListViewModel.SelectedDevice.TabViewModelsCollection, UpdateSourceTrigger=PropertyChanged}"

如果有用的话告诉我。
祝你好运

相关问题