WPF绑定(Binding)(4)

发布时间 2023-09-08 17:25:14作者: 秃头的C#

数据绑定

组件之间的绑定

<StackPanel>
            <Slider x:Name="sd" Width="200" />
            <TextBox Text="{Binding ElementName=sd, Path=Value}" />
        </StackPanel>

 

绑定数据源

<Window.Resources>
        <TextBox x:Key="txt">Hello</TextBox>
    </Window.Resources>
    <Grid>
        <StackPanel>

            <TextBox FontSize="30" Text="{Binding Source={StaticResource txt}, Path=Text}" />
        </StackPanel>
    </Grid>

 

绑定数据上下文

<TextBox
                x:Name="txt"
                FontSize="30"
                Text="{Binding Name, FallbackValue='Not Found'}" />

 public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = new Person() { Name = "abc" };
        }


    }

    public class Person
    {
        public string Name { get; set; }
    }

 

当前窗口的数据上下文

//新建文件MainViewModel
public class MainViewModel { public MainViewModel() { Name = "Hello"; } public string Name { get; set; } }
public MainWindow()
        {
            InitializeComponent();
            this.DataContext = new MainViewModel();
        }

实现双向绑定

public class MainViewModel : INotifyPropertyChanged
    {
        public MainViewModel()
        {
            Name = "Hello";
            Task.Run(async () => { 
                await Task.Delay(3000);
                Name = "No";
            });
        }

        private string name;
        public string Name
        {
            get { return name; }
            set { name = value; OnPropertyChanged("Name"); }
        }
        public event PropertyChangedEventHandler? PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

mvvmlight

 

public class MainViewModel : ViewModelBase
    {
        public MainViewModel()
        {
            Name = "Hello";
            Task.Run(async () => { 
                await Task.Delay(3000);
                Name = "No";
            });
        }

        private string name;
        public string Name
        {
            get { return name; }
            set { name = value; RaisePropertyChanged(); }
        }
       
    }

 

绑定事件

public class MainViewModel : ViewModelBase
    {
        public MainViewModel()
        {
            Name = "Hello";
            SaveCommand = new RelayCommand(() =>
            {
                Name = "No";
            });
        }

        public RelayCommand SaveCommand { get; private set; }

        private string name;
        public string Name
        {
            get { return name; }
            set { name = value; RaisePropertyChanged(); }
        }
       
    }
<Button
                Width="100"
                Height="40"
                Command="{Binding SaveCommand}"
                Content="click" />