wpf后台代码数据绑定

第一部分

参考文章:https://www.cnblogs.com/fdyang/p/3459309.html

1.  在UI(Xaml) 里面直接绑定数据.(不实用)【可运行】

<Window x:Class="WpfApplication1.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:WpfApplication1"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid Margin="10,10,282,280">
        <ComboBox x:Name="hello1" Margin="126,89,-126,-89">
            <ComboBoxItem>ComboBox Item #1</ComboBoxItem>
            <ComboBoxItem IsSelected="True">ComboBox Item #2</ComboBoxItem>
            <ComboBoxItem>ComboBox Item #3</ComboBoxItem>
        </ComboBox>
    </Grid>
</Window>

2. 动态绑定数据.

2.1 绑定XML到ComboBox【可运行,无数据】

     前台Xaml里面需要在Resource里面指定XmlDataProvider. 并设定绑定.  后台无需任何代码。

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
  <Window.Resources>
    <XmlDataProvider x:Key="DataProvider" Source="pack://siteoforigin:,,,/TestData.xml" XPath="Countries"/>
  </Window.Resources>
  <StackPanel>
    <ComboBox x:Name="com_Country" ItemsSource="{Binding Source={StaticResource DataProvider}, XPath=Country}" DisplayMemberPath="@Name" Margin="5"/>
    <ComboBox x:Name="com_City" DataContext="{Binding ElementName=com_Country, Path=SelectedItem}" ItemsSource="{Binding XPath=City}" DisplayMemberPath="@Name" Margin="5"/>
    <TextBlock Margin="5">
      <TextBlock.Inlines>
        <Run Text="Selected Country: "/>
        <Run Text="{Binding ElementName=com_Country, Path=SelectedItem.Attributes[Name].Value, Mode=OneWay}"/>
        <LineBreak/>        
        <Run Text="Selected City: "/>
        <Run Text="{Binding ElementName=com_City, Path=SelectedItem.Attributes[Name].Value}"/>
      </TextBlock.Inlines>
    </TextBlock>
  </StackPanel>
</Window>


3. 绑定Dictionary到ComboBox.

【原文章没有关联性,未完成】

第二部分

参考文章:http://www.cnblogs.com/mantian/archive/2013/01/22/2871775.html

1.ComboBox参数的解释


第三部分

参考文章:https://zhidao.baidu.com/question/408153485.html

1.在wpf中绑定comboBox的值,Dictionary【可运行】【后台绑定】放在public MainWindow下

前台:
 <ComboBox Name="combobox" Width="120" Height="30"/>
后台:
public MainWindow()
        {
            InitializeComponent();
            Dictionary<int, string> mydic = new Dictionary<int, string>()
            {
                {1,"a"},
                {2,"b"},
                {3,"c"}
            };
            combobox.ItemsSource = mydic;
            combobox.SelectedValuePath = "Key";
            combobox.DisplayMemberPath = "Value";
        }
ItemsSource 指定comboBox的数据源,可以是字典,list等任何形式的数据集合
SelectedValuePath 表示每个item的的实际值
DisplayMemberPath 表示每个item的显示值


2.在wpf中绑定comboBox的值,Dictionary【前台绑定】【自写】



第四部分

参考文章:

wpf数据绑定binding与INotifyPropertyChanged(可运行,文章第一部分是《深入浅出WPF》第10章的再深入延续;第二部分为书本10.1讲)

XAML文件:

<Window x:Class="InotifyChangedTest.Window1"
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
       xmlns:local ="clr-namespace:InotifyChangedTest"
   Title="Window1" Height="300" Width="300">
    <Grid>
        <TextBox Height="23" Margin="40,43,52,0" Name="textBox1" VerticalAlignment="Top" Text="{Binding Path=name}"/>
        <TextBox Height="23" Margin="40,94,52,0" Name="textBox2" VerticalAlignment="Top" Text="{Binding Path=desc}"/>
        <Button Height="23" Margin="106,0,97,42" Name="button1" VerticalAlignment="Bottom" Click="button1_Click">Button</Button>
    </Grid>
</Window>

TextBox 的Text是依赖属性。

cs文件,新手问题一般都比较多,我就把所有代码都复制过来了:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 InotifyChangedTest
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public person ps{get;set;}
        public Window1()
        {
            InitializeComponent();
            ps = new person();
            //注意这句,指定数据源
            this.DataContext = ps;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            ps.name += "a";
            ps.desc += "b";
        }

    }
//实现INotifyPropertyChanged接口,当数据改变时通知
    public class person:INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private string _name;
        public string name
        {
            get 
            {
                return _name;
            }
            set
            {
                if (value != _name)
                {
                    _name = value;
                    //改变时通知
                    prochanged("name");
                }
            }
        }
        private string _desc;
        public string desc
        {
            get
            {
                return _desc;
            }
            set
            {
                if (value != _desc)
                {
                    _desc = value;
                    //改变时进行通知
                    prochanged("desc");
                }
            }
        }
        
        private void prochanged(string info)
        {
            if (PropertyChanged != null)
            {
                //是不是很奇怪,这个事件发起后,处理函数在哪里?
                //我也不知道在哪里,我只知道,绑定成功后WPF会帮我们决定怎么处理。
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
    }
}
参考文章:

wpf后台代码数据绑定


参考文章:

WPF之数据绑定总结(全面)



猜你喜欢

转载自blog.csdn.net/sinat_37519884/article/details/79553484
今日推荐