You*_*ter 1 c# data-binding wpf xaml
是否有直接绑定到 DataContext 而不是它的属性的关键字?
我听说过使用自对象的解决方法。我的问题是我打开一个窗口,并给出一个 ObservableCollection 作为参数,它被设置为 DataContext。
这里是 WPF(xaml.cs) ctor
public Depot(ObservableCollection<ObservableCollection<ItemManager.Item>> totalDepot)
{
this.FullDepotList = totalDepot;
this.DataContext = FullDepotList[1];
InitializeComponent();
}
Run Code Online (Sandbox Code Playgroud)
我最好直接绑定到 DataContext 或绑定到“this”的 XAML 代码片段:
<WrapPanel>
<ListBox
ItemsSource="{Binding this, UpdateSourceTrigger=PropertyChanged}"
ItemTemplate="{DynamicResource ItemWithCoolTooltipTemplate}"
Focusable="False">
</ListBox>
</WrapPanel>
Run Code Online (Sandbox Code Playgroud)
要直接绑定到 DataContext 而不是绑定到它的属性,请不要编写任何绑定路径。让它只是{Binding}。UpdateSourceTrigger=PropertyChanged不需要,因为 ItemsSource 不会从视图中改变。
<ListBox
ItemsSource="{Binding}"
ItemTemplate="{DynamicResource ItemWithCoolTooltipTemplate}"
Focusable="False">
</ListBox>
Run Code Online (Sandbox Code Playgroud)
或者用于Path=.编码“在此处绑定整个 DataContext”要求
<ListBox
ItemsSource="{Binding Path=.}"
ItemTemplate="{DynamicResource ItemWithCoolTooltipTemplate}"
Focusable="False">
</ListBox>
Run Code Online (Sandbox Code Playgroud)
使用 RelativeSource/ElementName 的任何技巧通常都是更改绑定源所必需的。在这种情况下,DataContext(绑定源)只是从父 Window 继承而来。