有人知道WPF数据绑定示例的详尽集合吗?
似乎每次我读到一篇关于“如何进行WPF数据绑定”的文章时,都会使用一些不同的变体,有时使用DataContext,有时不使用,有时使用Itemssource或同时使用Itemssource和DataContext,还有ObjectDataProvider,您可以在XAML或codebehind中使用其中任何一种,或者没有代码隐藏,直接从XAML绑定到ViewModels XAML本身似乎有几十种不同的语法可供使用,例如:有人知道WPF数据绑定示例的详尽集合吗?,wpf,data-binding,Wpf,Data Binding,似乎每次我读到一篇关于“如何进行WPF数据绑定”的文章时,都会使用一些不同的变体,有时使用DataContext,有时不使用,有时使用Itemssource或同时使用Itemssource和DataContext,还有ObjectDataProvider,您可以在XAML或codebehind中使用其中任何一种,或者没有代码隐藏,直接从XAML绑定到ViewModels XAML本身似乎有几十种不同的语法可供使用,例如: <ListBox ItemsSource="{Binding Sou
<ListBox ItemsSource="{Binding Source={StaticResource Customers}}">
及
例如,这两个代码示例执行相同的操作:
1。使用无代码隐藏的ObjectDataProvider:
<Window x:Class="TestDataTemplate124.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestDataTemplate124"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<ObjectDataProvider x:Key="Customers"
ObjectType="{x:Type local:Customer}"
MethodName="GetAllCustomers"/>
</Window.Resources>
<StackPanel>
<ListBox DataContext="{StaticResource Customers}" ItemsSource="{Binding}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding FirstName}"/>
<TextBlock Text=" "/>
<TextBlock Text="{Binding LastName}"/>
<TextBlock Text=" ("/>
<TextBlock Text="{Binding Age}"/>
<TextBlock Text=")"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</Window>
<Window x:Class="TestDataTemplate123.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestDataTemplate123"
Title="Window1" Height="300" Width="300">
<StackPanel>
<ListBox x:Name="ListBox1">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding FirstName}"/>
<TextBlock Text=" "/>
<TextBlock Text="{Binding LastName}"/>
<TextBlock Text=" ("/>
<TextBlock Text="{Binding Age}"/>
<TextBlock Text=")"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</Window>
using System.Collections.ObjectModel;
using System.Windows;
namespace TestDataTemplate123
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
ListBox1.ItemsSource = Customer.GetAllCustomers();
}
}
}
2。没有数据上下文的示例:
<Window x:Class="TestDataTemplate124.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestDataTemplate124"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<ObjectDataProvider x:Key="Customers"
ObjectType="{x:Type local:Customer}"
MethodName="GetAllCustomers"/>
</Window.Resources>
<StackPanel>
<ListBox DataContext="{StaticResource Customers}" ItemsSource="{Binding}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding FirstName}"/>
<TextBlock Text=" "/>
<TextBlock Text="{Binding LastName}"/>
<TextBlock Text=" ("/>
<TextBlock Text="{Binding Age}"/>
<TextBlock Text=")"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</Window>
<Window x:Class="TestDataTemplate123.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestDataTemplate123"
Title="Window1" Height="300" Width="300">
<StackPanel>
<ListBox x:Name="ListBox1">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding FirstName}"/>
<TextBlock Text=" "/>
<TextBlock Text="{Binding LastName}"/>
<TextBlock Text=" ("/>
<TextBlock Text="{Binding Age}"/>
<TextBlock Text=")"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</Window>
using System.Collections.ObjectModel;
using System.Windows;
namespace TestDataTemplate123
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
ListBox1.ItemsSource = Customer.GetAllCustomers();
}
}
}
使用System.Collections.ObjectModel;
使用System.Windows;
命名空间TestDataTemplate123
{
公共部分类Window1:Window
{
公共窗口1()
{
初始化组件();
ListBox1.ItemsSource=Customer.GetAllCustomers();
}
}
}
是否有人知道解释WPF数据绑定的来源,而不是简单地说“这是您如何进行数据绑定的”,然后解释一种特定的方式,而是尝试解释进行数据绑定的各种方式,并显示可能的优缺点,例如,有无数据上下文,在XAML中绑定或代码隐藏等?我可以向您推荐这个博客。如果我没记错的话,她在微软工作,参与了WPF的开发,尤其是数据绑定。她真的有一些很棒的WPF教程,很多都是关于数据绑定的。你应该在这里找到一些非常好的信息。查看备忘单