Search code examples
wpfmvvmbindingdatacontexttabitem

How to switch dockpanels on binded object is null and not null, UpdateSourceTrigger not working i guess


I have a view model bound to my TabItem DataContext. And it shows all the UIContols contained in that dockpanel (Lets say dockpanel's name is DoockpanelWithdata). What i want to do is, when i have the value of the Viewmodel object equals to null then i want to show other Dockpanel saying there is no data(Lets say DoockpanelWithOutData).

My try is this:

How to switch these dockpanels when VM==null and not null ?

 <TabControl>
            <TabItem Name="Tab1" Cursor="Hand">                   
                <ListView Name="lZ" ItemsSource="{Binding UObj}" SelectedItem="{Binding SelectedItem ,Mode=TwoWay}"  >//On selected item i bind the the tabitem below which show different dockpanels on GM=null and not null 
              //columns here
                </ListView>
            </TabItem>
            <TabItem Name="TabGraph"  DataContext="{Binding GM , UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Cursor="Hand">                  
                <DockPanel Name="DoockpanelWithOutData">
                    <DockPanel.Style>
                        <Style TargetType="DockPanel">
                            <Setter Property="Visibility" Value="Collapsed" />
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding GM, UpdateSourceTrigger=PropertyChanged}" Value="{x:Null}">
                                    <Setter Property="Visibility" Value="Visible" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </DockPanel.Style>
                    <TextBlock>There IS NO DATA</TextBlock>
                </DockPanel>
                <DockPanel  Name="DoockpanelWithdata">
                    <DockPanel.Style>
                        <Style TargetType="DockPanel">
                            <Setter Property="Visibility" Value="Visible" />
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding GraphVM, UpdateSourceTrigger=PropertyChanged}" Value="{x:Null}">
                                    <Setter Property="Visibility" Value="Collapsed" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </DockPanel.Style>
                    //Some more data
                </DockPanel>
            </TabItem>
        </TabControl>

In View Model(UR1R2_VM.cs) i have :

private ObservableCollection<UModel> uObj; //binded to itemsource of another TabItem
 public ObservableCollection<UModel> UObj
        {
            get { return uObj; }
            set { uObj= value; OnPropertyChanged("UObj"); }
        }
        private UModel selectedItem; //Binded to listView selecteditem  of another tabitem        
    public UModel SelectedItem //HERE INSTANCE IS CREATED
    {
        get { return selectedItem; }
        set
        {
            selectedItem = value;
            GM = selectedItem != null ? new GM(selectedItem.Nom) : null; //HERE INSTANCE IS CREATED
            OnPropertyChanged("SelectedItem");                
        }
    }     


    private GM gM;
    public GM GM
    {
        get { return gM; }
        set
        {
            gM = value;
            OnPropertyChanged("GM");
        }
    }

Solution

  • Here you go..

    Just make sure that your GraphVM property is calling RaisePropertyChanged whenever it's changed (i.e., gets set to null). This is important in order for the trigger to get set.

    <DockPanel Name="DoockpanelWithOutData">
      <DockPanel.Style>
        <Style TargetType="DockPanel">
          <Setter Property="Visibility" Value="Collapsed" />
          <Style.Triggers>
            <DataTrigger Binding="{Binding DataContext, UpdateSourceTrigger=PropertyChanged}" Value="{x:Null}">
                <Setter Property="Visibility" Value="Visible" />
            </DataTrigger>
          </Style.Triggers>
        </Style>
      </DockPanel.Style>
      <TextBlock>There IS NO DATA</TextBlock>
    </DockPanel>
    <DockPanel  Name="DoockpanelWithdata">  
      <DockPanel.Style>
        <Style TargetType="DockPanel">
          <Setter Property="Visibility" Value="Visible" />
          <Style.Triggers>
            <DataTrigger Binding="{Binding DataContext, UpdateSourceTrigger=PropertyChanged}" Value="{x:Null}">
                <Setter Property="Visibility" Value="Collapsed" />
            </DataTrigger>
          </Style.Triggers>
        </Style>
      </DockPanel.Style>   
      //Some more data
    </DockPanel>