首页 > 其他分享 >【WPF】五、WPF绑定

【WPF】五、WPF绑定

时间:2022-08-28 22:14:36浏览次数:40  
标签:Windows 绑定 System Window4 using WPF public

<Window x:Class="WpfApp1.Window4"
        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:WpfApp1"
        mc:Ignorable="d"
        Title="Window4" Height="450" Width="800">
    <Grid>
        <StackPanel>
            <Slider x:Name="slider" Height="30"></Slider>
            <!--建立双向绑定当滑块值变动的时候,Text中的值也会发生变化,反之Text发生变化滑块也会变化-->
            <TextBox Height="30" Margin="5" Text="{Binding ElementName=slider,Path=Value,Mode=Default}"></TextBox>
            <!--绑定第一次便不再发生变化-->
            <TextBox Height="30" Margin="5" Text="{Binding ElementName=slider,Path=Value,Mode=OneTime}" ></TextBox>
            <!--单项绑定-->
            <TextBox Height="30" Margin="5" Text="{Binding ElementName=slider,Path=Value,Mode=OneWay}" ></TextBox>
            <!--双向绑定-->
            <TextBox Height="30" Margin="5" Text="{Binding ElementName=slider,Path=Value,Mode=TwoWay}" ></TextBox>
            <!--从输入框获取的绑定值,滑块没有影响,另一种单项绑定-->
            <TextBox Height="30" Margin="5" Text="{Binding ElementName=slider,Path=Value,Mode=OneWayToSource}" ></TextBox>
            
            
            <!--代码绑定方式1-->
            <TextBox Height="30" Margin="5" Text="{Binding Name}" ></TextBox>
            
        </StackPanel>

    </Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfApp1
{
    /// <summary>
    /// Window4.xaml 的交互逻辑
    /// </summary>
    public partial class Window4 : Window
    {
        public Window4()
        {
            InitializeComponent();
            this.DataContext = new Test()
            {
                Name = "张三"
            };

        }
    }

    public class Test
    {
        public string Name { get; set; }
    }
}

标签:Windows,绑定,System,Window4,using,WPF,public
From: https://www.cnblogs.com/xxxyz/p/16633825.html

相关文章