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

相关推荐
sealaugh322 小时前
aws(学习笔记第四十八课) appsync-graphql-dynamodb
笔记·学习·aws
水木兰亭2 小时前
数据结构之——树及树的存储
数据结构·c++·学习·算法
鱼摆摆拜拜3 小时前
第 3 章:神经网络如何学习
人工智能·神经网络·学习
lijingguang3 小时前
在C#中根据URL下载文件并保存到本地,可以使用以下方法(推荐使用现代异步方式)
开发语言·c#
aha-凯心3 小时前
vben 之 axios 封装
前端·javascript·学习
¥-oriented3 小时前
【C#中路径相关的概念】
开发语言·c#
ArabySide3 小时前
【WCF】通过AOP实现基于JWT的授权与鉴权的实践
c#·jwt·aop·wcf
xiaowu0804 小时前
C# Task异步的常用方法
c#
阿蒙Amon4 小时前
C# Linq to Objects 详解:集合处理的终极方案
c#·solr·linq
钢铁男儿4 小时前
C# 委托(调用带引用参数的委托)
java·mysql·c#