binding 里面local的用法

<Window x:Class="WpfApp4.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:WpfApp4"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <StackPanel Background="LightBlue">
        <StackPanel.DataContext>
            <local:Student Id="6" Age=" 29" Name="TIm"/>
            <!--StackPanel里面包着local,local根据xmnls指定的路径寻找到Student-->
        </StackPanel.DataContext>
        <Grid>
            <StackPanel>
                <TextBox Text="{Binding Path = Id }" Margin="5"/>
                <TextBox Text="{Binding Path = Name }" Margin="5"/>
                <TextBox Text="{Binding Path = Age }" Margin="5"/>
            </StackPanel>
        </Grid>
    </StackPanel>
</Window>
 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace WpfApp4
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    /// 


    public partial class MainWindow : Window
    {

        public MainWindow()
        {
 
            InitializeComponent();


            

        }
        public static string str1 = "";

    }

    public class Student
    {

        public int Id

        {
            set;
            get;
        }

        public string Name
        {
            set;
            get;
        }

        public int Age
        {

            set;
            get;
        }
    }
}
 

 

WPF 中,可以使用 Binding 绑定 Click 事件,但需要使用一个特殊的附加属性来实现。具体步骤如下: 1. 在 XAML 中定义 Button 控件,并使用 Binding 绑定 Click 事件,例如: ``` <Button Content="Click Me" local:ButtonClick.Command="{Binding MyCommand}" /> ``` 其中,ButtonClick 是一个自定义附加属性,可以在代码中定义,MyCommand 是一个 ICommand 类型的属性,表示要执行的命令。 2. 在代码中定义 ButtonClick 附加属性,并在其 PropertyChangedCallback 中注册 Click 事件的处理方法,例如: ``` public static class ButtonClick { public static readonly DependencyProperty CommandProperty = DependencyProperty.RegisterAttached("Command", typeof(ICommand), typeof(ButtonClick), new FrameworkPropertyMetadata(null, CommandChanged)); public static ICommand GetCommand(Button button) { return (ICommand)button.GetValue(CommandProperty); } public static void SetCommand(Button button, ICommand value) { button.SetValue(CommandProperty, value); } private static void CommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (d is Button button) { button.Click -= Button_Click; if (e.NewValue is ICommand command) { button.Click += Button_Click; } } } private static void Button_Click(object sender, RoutedEventArgs e) { if (sender is Button button) { ICommand command = GetCommand(button); if (command != null && command.CanExecute(button.CommandParameter)) { command.Execute(button.CommandParameter); } } } } ``` 其中,CommandProperty 是 ButtonClick 附加属性的依赖属性,GetCommand 和 SetCommand 方法用于获取和设置 Command 属性的值,CommandChanged 方法在 Command 属性值发生变化时注册或取消注册 Click 事件的处理方法,Button_Click 方法是 Click 事件的处理方法,用于执行绑定的命令。 注意,使用这种方式绑定 Click 事件时,Button 控件的 Click 事件不会触发,而是会触发 ButtonClick 附加属性的处理方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值