WPF学习(5) -- WPF绑定

一、双向绑定

1.代码示例

XML 复制代码
<Window x:Class="学习.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:学习"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">

    <Grid>
        <StackPanel>
            <Slider x:Name="slider" Margin="10"/>
            <TextBox Height="30"  Margin="10" Text="{Binding ElementName=slider,Path=Value}"/>
            <TextBox Height="30"  Margin="10" Text="{Binding ElementName=slider,Path=Value}"/>
            <TextBox Height="30"  Margin="10" Text="{Binding ElementName=slider,Path=Value}"/>
        </StackPanel>
    </Grid>
</Window>

2.代码结果

二、单向绑定

1.代码示例

XML 复制代码
<Window x:Class="学习.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:学习"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">

    <Grid>
        <StackPanel>
            <Slider x:Name="slider" Margin="10"/>
            <TextBox Height="30"  Margin="10" Text="{Binding ElementName=slider,Path=Value,Mode=OneWay}"/> <!--单向绑定-->
            <TextBox Height="30"  Margin="10" Text="{Binding ElementName=slider,Path=Value,Mode=OneTime}"/><!--绑定第一个数值-->
            <TextBox Height="30"  Margin="10" Text="{Binding ElementName=slider,Path=Value,Mode=TwoWay}"/><!--双向绑定-->
            <TextBox Height="30"  Margin="10" Text="{Binding ElementName=slider,Path=Value,Mode=OneWayToSource}"/><!--单向绑定-->
        </StackPanel>
    </Grid>
</Window>

2.代码结果

相关推荐
忒可君2 分钟前
C# winform FTP功能
开发语言·windows·c#
yiqiqukanhaiba12 分钟前
STM32学习笔记13-通信协议I2C&MPU6050&I2C软件控制
笔记·stm32·学习
Warren981 小时前
软件测试-Selenium学习笔记
java·javascript·笔记·学习·selenium·测试工具·安全
时光追逐者1 小时前
C#/.NET/.NET Core技术前沿周刊 | 第 50 期(2025年8.11-8.17)
c#·.net·.netcore·.net core
一个会的不多的人2 小时前
C# NX二次开发:操作按钮控件Button和标签控件Label详解
开发语言·c#
在路上`3 小时前
前端学习之后端小白java的一些理论知识(框架)
java·学习
咕白m6253 小时前
C# 实现 PDF 转图片 - 分辨率设置、图片格式选择
后端·c#
练习时长两年半的Java练习生(升级中)3 小时前
从0开始学习Java+AI知识点总结-18.web基础知识(Java操作数据库)
java·学习·web
Jayyih3 小时前
嵌入式系统学习Day19(数据结构)
数据结构·学习
xy_recording4 小时前
Day08 Go语言学习
开发语言·学习·golang