WPF中的数据绑定

数据绑定
以是声明方式将控件与数据关联到一起的方法
一个绑定(Bingding)关系又四个组件构成:
1.绑定目标
2.目标属性
3.绑定源
4.源属性

DataContext控件

定义一个数据源,该数据源可以绑定某个元素的所有子元素,常用一个类的实例来保存视图中大部分的数据,将窗口的DataContext设置为对象实例,从而将该类与视图中的属性绑起来。通常用于动态绑定到外部对象

绑定到本地对象

可绑定到任何包含所需数据的.net对象,只要编译器能够定位该对象。如果在使用对象的空间所在的上下文环境(既相同的xaml代码块)中可以找到该对象,就可以通过绑定的ElementName属性来指定绑定源。
<CheckBox Content="check" Name="checkbox"></CheckBox>
  <ComboBox  VerticalAlignment="Bottom" Name="combName" SelectedIndex="-1" IsEnabled="{Binding ElementName=checkbox,Path=IsChecked}">
     <ComboBoxItem>1</ComboBoxItem>
 </ComboBox>

1.绑定目标- 隐式指定为CombBox
2.目标属性 - IsEnable属性
3.绑定源 - checkBox
4.绑定属性 - IsChecked

绑定到外部对象

通过XAML将某个类指定为一项资源,就可以动态创建对象实例

①在xaml中添加名称空间,以便找到某个类
②在xaml的某个元素中,将类申明为资源

public class CodeRate: ObservableCollection<string>
{
    public CodeRate():base()
    {
        Add("128");
        Add("320");
        Add("flac");
       
    }
}

  <StackPanel Orientation="Horizontal">
      <StackPanel.Resources>
          <local:CodeRate x:Key="CodeRates"/>
      </StackPanel.Resources>
      <StackPanel.LayoutTransform>
                        <ScaleTransform ScaleX="1.2" ScaleY="1.2"></ScaleTransform>
                    </StackPanel.LayoutTransform>
      <Label Content="下载码率:" Foreground="White" Margin="5,0,0,0" VerticalAlignment="Center"></Label>
      <ComboBox Width="100" Margin="5"  ItemsSource="{Binding Source={StaticResource CodeRates}}"></ComboBox>
      </StackPanel">

通过itemsource 设置了绑定关系,在绑定中只需要指定绑定源,绑定目标,目标属性,绑定源和都在itemsource在进行处理。

动态绑定到外部属性

后台代码
 public class BindableBase : INotifyPropertyChanged
    {
        protected void SetProperty<T>(ref T prop, T value, [CallerMemberName] string callerName = null)
        {
            if (!EqualityComparer<T>.Default.Equals(prop, value))
            {
                prop = value;
                OnPropertyChanged(callerName);
            }
        }

        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
   [Serializable]
    public class ViewInfo : BindableBase
    {
        [NonSerialized]
        private string _searchTerm;
        public string SearchTerm
        {
            get => _searchTerm;
            set => SetProperty(ref _searchTerm, value);
        }
        private bool _QQSource = true;
        public bool QQSource
        {
            get => _QQSource;
            set => SetProperty(ref _QQSource, value);
        }

        private bool _KugouSource = true;
        public bool KugouSource
        {
            get => _KugouSource;
            set => SetProperty(ref _KugouSource, value);
        }

        private bool _NeteasySource = true;
        public bool NeteasySource
        {
            get => _NeteasySource;
            set => SetProperty(ref _NeteasySource, value);
        }
        private string _codeRate= "flac";
        public string CodeRate
        {
            get => _codeRate;
            set => SetProperty(ref _codeRate, value);
        }

        private string _downPath= Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "\\Music\\";
        public string DownPath
        {
            get => _downPath;
            set=>SetProperty(ref _downPath, value);
        }
       
    }
        private ViewInfo _viewInfo;
        private readonly string configAppName = "DownMusic";
        public MainWindow()
        {
            将界面设置写入到注册表,保存用户设置
            if(_viewInfo == null)
            {
                string config = ReadWriteRegister.ReadConfigurationAppSettings
                    (configAppName);
                if(config!=null)
                {
                    _viewInfo = JsonConvert.DeserializeObject<ViewInfo>
                        (config);
                }
                else
                {
                    _viewInfo = new ViewInfo();
                }
            }
            InitializeComponent();
            DataContext = _viewInfo;

        }
        //关闭窗口时,保存用户设置
        private void Window_Closed(object sender, EventArgs e)
        {
            string config = JsonConvert.SerializeObject(_viewInfo);
            if(config!=null)
            {
                ReadWriteRegister.SaveConfigurationAppSettings(configAppName, config);
            }
        }

XAML绑定代码:
在目标属性上绑定源属性,即可完成绑定

            <TextBox Width="300" TextAlignment="Left"  IsEnabled="False" Margin="5" Name="txtPath" Text="{Binding DownPath}"></TextBox>
            <Button Content="  浏览  " Margin="4" Name="btnView" Click="btnView_Click"></Button>

前端绑定可以学习

https://blog.csdn.net/fwj380891124/article/details/8107646

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值