簡體   English   中英

當特定 state 更改時更新 TabControl ItemSource ViewModels

[英]Updating TabControl ItemSource ViewModels when specific state changes

我有一個TabControl綁定到視圖模型的ObservableCollection屬性TabViewModelsCollection

當從視圖模型中設置另一個屬性DeviceState時,我想引發一個屬性更改事件並告訴我的TabControl s ItemSource刷新。

問題是我的ItemSourceViewModelsObservableCollection ,當我調用RaisePropertyChanged("TabViewModelsCollection"); 什么都沒有更新。

此外,我的選項卡視圖包含多個用戶控件和綁定。

應該播放的場景是:設備位於網絡上,收集數據,然后更新設備信息選項卡。

目前我的TabControl僅在我 select 一個不同的設備然后 select 我想查看的設備時更新。 考慮左側面板的設備列表,右側面板帶有設備信息選項卡。

讓我知道你們可能想看代碼的哪一部分,我的代碼庫很大,所以很難發布。

這是在我的視圖中定義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");

嗨,請嘗試下一個解決方案:

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}"

讓我知道是否有幫助。

問候。

我遇到了類似的問題。

OP有沒有解決這個問題?

當您更改 ViewModel 並因此通過關聯將 View 綁定到它時,您做了什么來刷新當前選項卡? 對我來說,用戶必須單擊當前選項卡以強制綁定視圖的控件出現在其上。

謝謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM