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

相关推荐
枫叶kx1 小时前
1Panel运行的.net程序无法读取系统字体(因为使用了docker)
c#
军训猫猫头6 小时前
96.如何使用C#实现串口发送? C#例子
开发语言·c#
恰薯条的屑海鸥7 小时前
零基础在实践中学习网络安全-皮卡丘靶场(第十六期-SSRF模块)
数据库·学习·安全·web安全·渗透测试·网络安全学习
喜欢吃燃面7 小时前
C++刷题:日期模拟(1)
c++·学习·算法
不爱写代码的玉子8 小时前
HALCON透视矩阵
人工智能·深度学习·线性代数·算法·计算机视觉·矩阵·c#
2301_797604248 小时前
学习记录:DAY32
学习
蓝婷儿9 小时前
6个月Python学习计划 Day 16 - 面向对象编程(OOP)基础
开发语言·python·学习
叶子2024229 小时前
学习使用YOLO的predict函数使用
人工智能·学习·yolo
jackson凌9 小时前
【Java学习笔记】SringBuffer类(重点)
java·笔记·学习
明耀10 小时前
WPF DataGrid 默认显示行号
wpf