美文网首页
ObjectDataProviderDemo

ObjectDataProviderDemo

作者: sstraybitd | 来源:发表于2020-11-21 22:52 被阅读0次
image.png
<Window x:Class="ObjectDataProviderDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ObjectDataProviderDemo"
        xmlns:system="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">

    <Window.Resources>
        <ObjectDataProvider x:Key="myDataSource" ObjectType="{x:Type local:Person}">
            <ObjectDataProvider.ConstructorParameters>
                <system:String>Joe</system:String>
            </ObjectDataProvider.ConstructorParameters>
        </ObjectDataProvider>
        <Style TargetType="{x:Type Label}">
            <Setter Property="DockPanel.Dock" Value="Top"/>
            <Setter Property="FontSize" Value="12"/>
        </Style>
        <Style TargetType="{x:Type TextBox}">
            <Setter Property="Width" Value="100"/>
            <Setter Property="Height" Value="25"/>
            <Setter Property="DockPanel.Dock" Value="Top"/>
        </Style>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="Width" Value="100"/>
            <Setter Property="Height" Value="25"/>
            <Setter Property="DockPanel.Dock" Value="Top"/>
        </Style>
    </Window.Resources>
    <Border Margin="25" BorderBrush="Aqua" BorderThickness="3" Padding="8">
        <DockPanel Width="200" Height="100">
            <Label>Enter a Name:</Label>
            <TextBox>
                <TextBox.Text>
                    <Binding Source="{StaticResource myDataSource}" Path="Name"
                   UpdateSourceTrigger="PropertyChanged"/>
                </TextBox.Text>
            </TextBox>

            <Label>The name you entered:</Label>
            <TextBlock Text="{Binding Source={StaticResource myDataSource}, Path=Name}"/>
        </DockPanel>
    </Border>
</Window>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace ObjectDataProviderDemo
{
    class Person : INotifyPropertyChanged
    {
        private string name;
        // Declare the event
        public event PropertyChangedEventHandler PropertyChanged;

        public Person()
        {
        }

        public Person(string value)
        {
            this.name = value;
        }

        public string Name
        {
            get { return name; }
            set
            {
                name = value;
                // Call OnPropertyChanged whenever the property is updated
                OnPropertyChanged();
            }
        }

        // Create the OnPropertyChanged method to raise the event
        // The calling member's name will be used as the parameter.
        protected void OnPropertyChanged([CallerMemberName] string name = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
        }
    }
}

参考:
1、https://docs.microsoft.com/en-us/dotnet/api/system.windows.data.objectdataprovider.constructorparameters?view=net-5.0
2、https://docs.microsoft.com/en-us/dotnet/desktop/wpf/data/how-to-implement-property-change-notification?view=netframeworkdesktop-4.8&viewFallbackFrom=netdesktop-5.0

相关文章

网友评论

      本文标题:ObjectDataProviderDemo

      本文链接:https://www.haomeiwen.com/subject/jwidiktx.html