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.代码结果

相关推荐
c#上位机36 分钟前
wpf之命令
c#·wpf
没有bug.的程序员1 小时前
分布式链路追踪:微服务可观测性的核心支柱
java·分布式·微服务·架构·wpf
程序员东岸2 小时前
避坑修链表:从顺序表到单链表的那点事儿(含可跑示例与小项目串联)
数据结构·笔记·学习·程序人生·链表
future14122 小时前
C++ 学习日记
开发语言·c++·学习
禁默2 小时前
机器学习基础入门(第五篇):半监督学习与强化学习
人工智能·学习·机器学习
懒羊羊不懒@2 小时前
算法入门数学基础
c语言·数据结构·学习·算法
zhonghaoxincekj2 小时前
晶体管的定义,晶体管测量参数和参数测量仪器
功能测试·单片机·学习·测试工具·单元测试·制造
从前慢,现在也慢2 小时前
(3)Bug篇
学习·bug·测试
努力努力再nuli3 小时前
学习华为昇腾AI教材图像处理原理与应用部分Day1
图像处理·人工智能·学习
Freya冉冉3 小时前
【PYTHON学习】推断聚类后簇的类型DAY18
python·学习·聚类