简体   繁体   English

当特定 state 更改时更新 TabControl ItemSource ViewModels

[英]Updating TabControl ItemSource ViewModels when specific state changes

I have a TabControl bound to an ObservableCollection property of view models, TabViewModelsCollection .我有一个TabControl绑定到视图模型的ObservableCollection属性TabViewModelsCollection

When another property gets set from within the view models, DeviceState , I'd like to raise a property changed event and tell my TabControl s ItemSource to refresh.当从视图模型中设置另一个属性DeviceState时,我想引发一个属性更改事件并告诉我的TabControl s ItemSource刷新。

The problem is my ItemSource is an ObservableCollection of ViewModels and when I call RaisePropertyChanged("TabViewModelsCollection");问题是我的ItemSourceViewModelsObservableCollection ,当我调用RaisePropertyChanged("TabViewModelsCollection"); nothing gets updated.什么都没有更新。

Furthermore my tab views contain multiple user controls and bindings.此外,我的选项卡视图包含多个用户控件和绑定。

The scenario that is supposed to play out is: A device is located on the network, data is collected, and the tabs of device information should then be updated.应该播放的场景是:设备位于网络上,收集数据,然后更新设备信息选项卡。

Currently my TabControl only updates when I select a different device then select the device whos information I want to see.目前我的TabControl仅在我 select 一个不同的设备然后 select 我想查看的设备时更新。 Think left panel list of devices, right panel with tabs of device info.考虑左侧面板的设备列表,右侧面板带有设备信息选项卡。

Let me know what part of the code you guys might want to see, my codebase is quite large so it would be hard to post it.让我知道你们可能想看代码的哪一部分,我的代码库很大,所以很难发布。

Here is where the TabControl is defined in my view:这是在我的视图中定义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>

EDIT: I want to be able to call raised property changed on this and have it refresh all of my tab views..编辑:我希望能够调用已更改的引发属性并让它刷新我的所有选项卡视图..

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

RaisePropertyChanged("TabViewModelsCollection");

Hi please try the next solution: 嗨,请尝试下一个解决方案:

VM code redaction 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 redaction (define the UpdateSourceTrigger) Xaml编辑(定义UpdateSourceTrigger)

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

Let me know if it was helpful. 让我知道是否有帮助。

Regards. 问候。

I'm experiencing a similar problem.我遇到了类似的问题。

Did the OP ever resolve this? OP有没有解决这个问题?

What did you do to make the current tab refresh when you changed the ViewModel, and thus, by association, the View bound to it?当您更改 ViewModel 并因此通过关联将 View 绑定到它时,您做了什么来刷新当前选项卡? For me the user has to click on the current tab to force the bound View's controls to appear on it.对我来说,用户必须单击当前选项卡以强制绑定视图的控件出现在其上。

Thanks谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当 TabControl 绑定到 ViewModels 时 WPF 很慢 - WPF slow when TabControl bound to ViewModels 在TabControl中将StaticResource与ItemSource混合 - StaticResource mixed with ItemSource in TabControl 引用 itemsource 时,在 tabcontrol 上使用 tabindex 的样式触发器不起作用 - Style Trigger using tabindex on tabcontrol is not working when itemsource is referenced 当 observablecollection ItemSource 更改 WPF 时,通过 MVVM 以编程方式关注行中的特定单元格 - Focus on specific cell in row programmatically via MVVM when observablecollection ItemSource changes WPF 将包装的视图模型绑定到TabControl - Binding wrapped viewmodels to TabControl 在TabControl中使用各种ViewModel重用UserControl - Reuse UserControl in TabControl with Various ViewModels Mahapps TabControl,在使用ItemSource = {Binding ..}时无法将CloseButtonEnabled = true设置 - Mahapps TabControl, can't set CloseButtonEnabled = true when using ItemSource ={Binding..} WPF - 当ComboBox ItemSource更改时还原以前的SelectedItem - WPF - Restore Previous SelectedItem when ComboBox ItemSource Changes 关闭没有项源的Tabcontrol的虚拟化-WPF - Turning off Virtualization for Tabcontrol without itemsource - WPF Xamarin.Forms - 更新ItemSource时,ViewCell内的图像闪烁 - Xamarin.Forms - Image inside ViewCell flashes when updating the ItemSource
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM