"WPF 데이터 바인딩 방법"에 대한 기사를 읽을 때마다 때때로 DataSource, 때때로 ItemSource 또는 ItemSource와 DataContext와 함께 몇 가지 다른 변형으로 처리되는 것으로 보입니다. 또한 ObjectDataProvider도 있습니다. XAML 또는 코드 숨김에서 이들 중 하나를 가질 수도 있고 코드 숨김없이 XAML에서 직접 ViewModel에 바인딩 할 수도 있습니다.WPF 데이터 바인딩 예제의 철저한 컬렉션을 알고있는 사람이 있습니까?
XAML 자체 내에서 사용하는 다른 구문의 수십, 예를 들어 있습니다 것 같다 :
<ListBox ItemsSource="{Binding Source={StaticResource Customers}}">
및
<ListBox DataContext="{StaticResource Customers}" ItemsSource="{Binding}">
이 두 코드 샘플, 예를 들어, 같은 일을이 :
:1. 어떤 코드 숨김으로 ObjectDataProvider 사용 아무의 DataContext와
<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>
2. 예 : 다음 하나의 특정 방법을 설명
<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();
}
}
}
사람이 대신 말의에 의해 WPF 데이터 바인딩을 설명하는 소스를 알고 있나요은, "당신은 데이터 바인딩을 어떻게 여기에" 대신에 에 시도하여 다양한 방법으로 데이터 바인딩에 대해 설명하고 어떤 장단점을 보여 주는지 예 DataContext를 가지고 있건 없건간에, XAML이나 코드 숨김 등에서 바인딩?