wpf - 有人知道 WPF 数据绑定(bind)示例的详尽集合吗?

标签 wpf data-binding

似乎每次我读到一篇关于“如何进行WPF数据绑定(bind)”的文章时,它都是用一些不同的变体来完成的,有时使用DataContext,有时不使用,有时使用Itemssource或同时使用Itemssource和DataContext,还有ObjectDataProvider,您可以在 XAML 或代码隐藏中使用其中任何一个,或者没有代码隐藏并直接从 XAML 绑定(bind)到您的 ViewModel。

XAML 本身似乎有数十种不同的语法可供使用,例如:

<ListBox ItemsSource="{Binding Source={StaticResource Customers}}">

<ListBox DataContext="{StaticResource Customers}" ItemsSource="{Binding}">

例如,这两个代码示例执行相同的操作:

<强>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>

<强>2。没有 DataContext 的示例:

<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 数据绑定(bind)的来源,而不是仅仅说“这是如何进行数据绑定(bind)”,然后解释一种特定的方式,而是尝试解释进行数据绑定(bind)的各种方法并可能显示例如,有什么优点和缺点是否有 DataContext、是否绑定(bind)在 XAML 或代码隐藏等中?

最佳答案

查看this备忘单

关于wpf - 有人知道 WPF 数据绑定(bind)示例的详尽集合吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/793158/

相关文章:

javascript - Knockout.JS $element 数据绑定(bind)变量未定义?

c# - 数据绑定(bind)到 WPF 中的文本框

c# - WPF, TreeView 选择更改

wpf - ScrollViewer 中的用户控件不会在 WPF 中滚动

wpf - 在事件 Keydown 中获取发件人文本框名称

c# - 如何使用 UIElement 依赖属性实现 WPF 用户控件?

java - 使用数据绑定(bind)时,RecyclerView 项目属性会自动更改

javascript - jQuery 模板插件 : how to create two-way binding?

c# - WPF 复合控件

c# - 如何在运行时更改 SolidColorBrush 资源的颜色?