WPF DataContext與Binding的關系


在前台UI創建一個Label綁定到myLabel

<Window x:Class="WpfApp1.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:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="300" Width="500">
    <Grid>
        <Label Content="{Binding myLabel}"/>
    </Grid>
</Window>

在后台代碼賦值myLabel

using System.Windows;

namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private string label = "我的標簽";
        public string myLabel
        {
            get { return label; }
            set { label = value; }
        }
        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;
        }
    }
}

運行如下

 

 這里的DataContext = this到底是什么呢?調試運行發現DataContext 其實就是MainWindow類

 

 如果我們自己新建一個類,讓DataContext 等於這個新建的類是否可以呢?

 

 內容如下

namespace WpfApp1
{
    public class ClassA
    {
        private string label = "新建類A的標簽";
        public string myLabel
        {
            get { return label; }
            set { label = value; }
        }
    }
}

MainWindow.xaml.cs里的DataContext = this改成 DataContex = new ClassA();其他的不變

    public partial class MainWindow : Window
    {
        private string label = "我的標簽";
        public string myLabel
        {
            get { return label; }
            set { label = value; }
        }
        public MainWindow()
        {
            InitializeComponent();
            //DataContext = this;
            DataContext = new ClassA();
        }
    }

運行結果

 源碼下載地址https://github.com/lizhiqiang0204/DataContext-and-Bingding.git

有朋友會有疑問,一個UI文件的DataContext只能指定一個類嗎?不,是可以指定多個類的。

我們新建一個類ClassB

 

 其他文件不用動,只需修改UI文件

<Window x:Class="WpfApp1.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:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="300" Width="500">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid>
            <Grid.DataContext>
                <local:ClassA/>
            </Grid.DataContext>
            <Label Content="{Binding myLabel}"/>
        </Grid>
        <Grid Grid.Row="1">
            <Grid.DataContext>
                <local:ClassB/>
            </Grid.DataContext>
            <Label Content="{Binding myLabel}"/>
        </Grid>
    </Grid>
</Window>

 多類之間如何互相訪問呢?參考https://www.cnblogs.com/lizhiqiang0204/p/12582510.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM