WPF之绑定

绑定的几种方式

1 元素绑定

    <Grid>
        <StackPanel >
            <TextBox x:Name="t1" HorizontalAlignment="Left" Margin="35,26,0,0" TextWrapping="Wrap"   Height="25" Width="266"/>
             <!--元素绑定:绑定Name值为t1的元素,Path=Text表示将这个元素值的Text值赋给当前元素的Text属性-->
            <TextBox Text="{Binding Text, ElementName=t1}" HorizontalAlignment="Left" Margin="35,71,0,0"   VerticalAlignment="Top" Height="26" Width="266"/>
          
        </StackPanel>
    </Grid>

 

RelativeSource绑定

3 绑定后台业务代码

比如后台有如下类:

复制代码
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            //注意要在这里将数据添加到上下文中
            this.DataContext = new TestModel();
        }

    }
    public class TestModel
    {
        public TestModel()
        {
            Name = "Hello";
        }
        private string name;

        public string Name { get => name; set => name = value; }
    }
}
复制代码

 

xaml中绑定:

复制代码
<Window x:Class="MyWpf.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" 
        mc:Ignorable="d"  
        Title="MainWindow" Height="296.042" Width="347.333"  > 
    <Grid>
        <StackPanel > 
            <TextBlock Text="{Binding Name,FallbackValue='error'}" HorizontalAlignment="Left" Margin="35,71,0,0" 
VerticalAlignment
="Top" Height="26" Width="266"/> </StackPanel> </Grid> </Window>
复制代码

 

我们还可以换一种方式写,用强类型视图的方式来写,直接在xaml文件中指定当前上下文用什么模型:

复制代码
<Window x:Class="MyWpf.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" 
        mc:Ignorable="d" 
    xmlns:local="clr-namespace:MyWpf"
        Title="MainWindow" Height="296.042" Width="347.333"  >
    <Window.DataContext>
        <!--之所在这里能直接这样写,是因为前面属性中引入了这个类的命名空间,  xmlns:local="clr-namespace:MyWpf" -->
        <local:TestModel></local:TestModel>
    </Window.DataContext>
    <Grid>
        <StackPanel >

            <TextBlock Text="{Binding Name,FallbackValue='error'}" HorizontalAlignment="Left" Margin="35,71,0,0"   
VerticalAlignment
="Top" Height="26" Width="266"/> </StackPanel> </Grid> </Window>
复制代码

 

这样写的话,在Binding中就会有提示有哪些属性是可以被绑定的。

 

 

 

 

 

 

 

 

 

 

 

 

1

posted @   安静点--  阅读(268)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示