[英]WPF Binding directly to the DataContext
是否有直接綁定到 DataContext 而不是它的屬性的關鍵字?
我聽說過使用自對象的解決方法。 我的問題是我打開一個窗口,並給出一個 ObservableCollection 作為參數,它被設置為 DataContext。
這里是 WPF(xaml.cs) ctor
public Depot(ObservableCollection<ObservableCollection<ItemManager.Item>> totalDepot)
{
this.FullDepotList = totalDepot;
this.DataContext = FullDepotList[1];
InitializeComponent();
}
我最好直接綁定到 DataContext 或綁定到“this”的 XAML 代碼片段:
<WrapPanel>
<ListBox
ItemsSource="{Binding this, UpdateSourceTrigger=PropertyChanged}"
ItemTemplate="{DynamicResource ItemWithCoolTooltipTemplate}"
Focusable="False">
</ListBox>
</WrapPanel>
要直接綁定到 DataContext 而不是綁定到它的屬性,請不要編寫任何綁定路徑。 讓它只是{Binding}
。 UpdateSourceTrigger=PropertyChanged
不是必需的,因為 ItemsSource 不會從視圖中改變。
<ListBox
ItemsSource="{Binding}"
ItemTemplate="{DynamicResource ItemWithCoolTooltipTemplate}"
Focusable="False">
</ListBox>
或者使用Path=.
編碼“在此處綁定整個 DataContext”要求
<ListBox
ItemsSource="{Binding Path=.}"
ItemTemplate="{DynamicResource ItemWithCoolTooltipTemplate}"
Focusable="False">
</ListBox>
使用 RelativeSource/ElementName 的任何技巧通常都是更改綁定源所必需的。 在這種情況下,DataContext(綁定源)只是從父 Window 繼承而來。
您可以嘗試以下技巧。 將 name 屬性添加到您的 Window - <Window ... Name="myWindow" ...>
使用這樣的構造來綁定到該屬性或您需要的任何內容 - <ListBox ItemsSource="{Binding Path=DataContext, ElementName=myWindow}" ... />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.