我有一個綁定到我的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");
}
}
您的代碼對我來說工作正常,但是您是否確保爲'DockPanelWithData'的內容設置DataContext?默認的DataContext將是你的'UR1R2_VM',而不是你的'Graph_VM'對象。除此之外,您可以在設置GraphVM的位置放置斷點以確保將其設置爲期望值? – Rachel
我不知道你是否注意到,在我的第一個tabitem我已經綁定列表視圖選擇項目SelectedItem屬性在我的UR1R2_VM viewmodel。在選擇一行時實例化GraphVM對象。包含兩個dockpanel的第二個ab設置爲GraphVM類的datacontext。 –