wpf 的binding 方式

参考:WPF中的Binding的常见知识点与技巧_哔哩哔哩_bilibili

总结了一些:wpf中的binding的方式。

<Window x:Class="WpfBindingDemo.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"

FontSize="20"

xmlns:sys="clr-namespace:System;assembly=mscorlib"

xmlns:local="clr-namespace:WpfBindingDemo"

mc:Ignorable="d"

x:Name="myWindow"

Title="MainWindow" Height="1050" Width="800">

<Window.DataContext>

<local:MainViewModel></local:MainViewModel>

</Window.DataContext>

<Window.Resources>

<sys:String x:Key="test">测试数据</sys:String>

<local:MyResource x:Key="myres"></local:MyResource>

</Window.Resources>

<Grid>

<StackPanel Margin="10" Orientation="Vertical" VerticalAlignment="Top">

<TextBox Text="{DynamicResource test}" x:Name="txtName" ></TextBox>

<TextBlock Text="{Binding Source={StaticResource test} }" x:Name="txtTest" Width="460" Height="40"></TextBlock>

<TextBlock Text="{StaticResource test}" Width="460" Height="40"></TextBlock>

<TextBlock Text="{DynamicResource test}" Width="460" Height="40"></TextBlock>

<TextBlock Text="{Binding Source={StaticResource myres},Path=Message}" Width="460" Height="40"></TextBlock>

<TextBlock Text="{Binding Source={x:Static local:MyResource.ConstString}}" Width="460" Height="40"></TextBlock>

<TextBlock Text="{Binding Source={x:Static local:MyResource.StaticString}}" Width="460" Height="40"></TextBlock>

<TextBlock Text="{Binding Source={x:Static local:MyEnum.Message}}" Width="460" Height="40"></TextBlock>

<TextBlock Text="{Binding ElementName=txtName, Path=Text}" Width="460" Height="40"></TextBlock>

<TextBlock Text="{Binding ElementName=txtName,Path=Text}">

<TextBlock.ToolTip>

<TextBlock Text="{Binding ElementName=txtName,Path=Text}"></TextBlock><!--没有提示-->

</TextBlock.ToolTip>

</TextBlock>

<TextBlock Text="{Binding ElementName=txtName,Path=Text}">

<TextBlock.ToolTip>

<TextBlock Text="{Binding Source={x:Reference Name=txtName},Path=Text}"></TextBlock> <!--ok-->

</TextBlock.ToolTip>

</TextBlock>

<Button x:Name="btnTest" Width="100" Content="Click"></Button>

<DataGrid>

<DataGrid.Columns>

<DataGridTextColumn Header="ID"></DataGridTextColumn>

<DataGridTextColumn Header="{Binding ElementName=txtName,Path=Text}"></DataGridTextColumn><!--没有提示-->

<DataGridTextColumn Header="Class"></DataGridTextColumn>

<DataGridTextColumn Header="{Binding Source={x:Reference Name=txtName },Path=Text}"></DataGridTextColumn><!--ok-->

</DataGrid.Columns>

</DataGrid>

<TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Window},Path=Top,StringFormat='Top:{0:F3}'}"></TextBlock>

<TextBlock Text="{Binding RelativeSource={RelativeSource Mode=Self},Path=ActualHeight,StringFormat='Height:{0:C}'}"></TextBlock>

<TextBlock>

<TextBlock.Text>

<MultiBinding StringFormat="Pos:{0:F2},{1:F2}">

<Binding ElementName="myWindow" Path="Top"></Binding>

<Binding ElementName="myWindow" Path="Left"></Binding>

</MultiBinding>

</TextBlock.Text>

</TextBlock>

<Label Content="{Binding RelativeSource={RelativeSource Mode=Self},Path=ActualHeight}" ContentStringFormat="Lable Height:{0:F0}"></Label>

<ListBox>

<TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem},Path=IsSelected}"></TextBlock>

<TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem},Path=IsSelected}"></TextBlock>

<TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem},Path=IsSelected}"></TextBlock>

</ListBox>

<Label Padding="2" Content="测试label" Height="30">

<Label.Template>

<ControlTemplate TargetType="Label">

<Border BorderBrush="Red" BorderThickness="1" Margin="{Binding RelativeSource={RelativeSource Mode=TemplatedParent},Path=Padding}">

<ContentPresenter></ContentPresenter>

</Border>

</ControlTemplate>

</Label.Template>

</Label>

<TextBlock Text="{Binding MessageKong,TargetNullValue=空}"></TextBlock>

<TextBlock Text="{Binding MessageKong2,FallbackValue=MessageKong2不存在}"></TextBlock>

</StackPanel>

</Grid>

</Window>

类文件

namespace WpfBindingDemo

{

/// <summary>

/// Interaction logic for MainWindow.xaml

/// </summary>

public partial class MainWindow : Window

{

public MainWindow()

{

InitializeComponent();

this.Resources["test"] = DateTime.Now.ToString();

}

}

class MyResource

{

public string Message { get; set; } = "test 哈哈";

public static string StaticString = "static string";

public const string ConstString = "Const string";

}

public enum MyEnum

{

Message,

OK,

Error

}

MainViewModel.cs

namespace WpfBindingDemo

{

public class MainViewModel

{

public string Name { get; set; }

public string Message { get; set; } = "test 哈哈";

public string MessageKong { get; set; }

}

}

相关推荐
创可贴治愈心灵41 分钟前
WPF中UI线程频繁操作造成卡顿的处理
ui·c#·wpf
我希望的一路生花7 小时前
Adobe Photoshop 2025 版本介绍与使用指南
ui·adobe·photoshop
Zafir202420 小时前
Qt实现TabWidget通过addTab函数添加的页,页内控件自适应窗口大小
开发语言·c++·qt·ui
阿登林20 小时前
初步学习WPF-Prism
学习·wpf
兰亭妙微1 天前
审美积累 | 界面设计拆分 | Redesign Health - Services 医疗页面设计
ui·用户体验·医疗界面设计
格林威1 天前
Baumer高防护相机如何通过YoloV8深度学习模型实现网球运动员和网球速度的检测分析(C#代码UI界面版)
人工智能·深度学习·数码相机·yolo·ui·c#·视觉检测
△曉風殘月〆1 天前
WPF MVVM进阶系列教程(三、使用依赖注入)
wpf·mvvm
9084869051 天前
产品设计.原型设计
ui·原型设计
此wei浩亦1 天前
WPF中使用 using prism.region 报错
c#·wpf·prism
四维碎片2 天前
【Qt】线程池与全局信号实现异步协作
开发语言·qt·ui·visual studio