2017-01-26 56 views
0

我有一個綁定到我的TabItem DataContext的視圖模型。它顯示了該dockset中包含的所有UIContols(可以說Dockpanel的名字是DoockpanelWithdata)。 我想要做的是,當我有Viewmodel對象的值等於空然後我想顯示其他Dockpanel說沒有數據(讓我們說DoockpanelWithOutData)。如何切換綁定對象上的dockpanels爲null並非null,UpdateSourceTrigger無法正常工作我猜

我的嘗試是這樣的:

如何切換這些dockpanels當VM == null和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> 

在視圖模型(UR1R2_VM.cs)我有:

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"); 
     } 
    } 
+0

您的代碼對我來說工作正常,但是您是否確保爲'DockPanelWithData'的內容設置DataContext?默認的DataContext將是你的'UR1R2_VM',而不是你的'Graph_VM'對象。除此之外,您可以在設置GraphVM的位置放置斷點以確保將其設置爲期望值? – Rachel

+0

我不知道你是否注意到,在我的第一個tabitem我已經綁定列表視圖選擇項目SelectedItem屬性在我的UR1R2_VM viewmodel。在選擇一行時實例化GraphVM對象。包含兩個dockpanel的第二個ab設置爲GraphVM類的datacontext。 –

回答

0

在這裏,你去..

只要確保每當它改變了你的GraphVM屬性是在調用RaisePropertyChanged(即被設置爲空)。這對觸發器的設置非常重要。

<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> 
+0

我覺得UpdateSourceTrigger = PropertyChanged不起作用。在兩種情況下,當GraphVM初始化且未初始化時,它仍然顯示DoockpanelWithOutData。 –

+0

您的DataContext錯誤,或者您的VM屬性設置不正確。需要更多的代碼?發佈你的相關虛擬機和虛擬機屬性 –

+0

我已經更新了代碼,請參閱 –

相關問題