在这个例子中,在 XAML 中使用 DataContext 和隐藏代码有什么区别?
What's the difference in this example between using DataContext in XAML & code behind?
我正在学习 WPF 中与 DataContext 的绑定方式,但我不明白为什么它会这样运行。
以下是代码。
Ex1:可以得到正确的绑定数据(在代码后面赋值DataContext)
Xaml
<Window x:Class="DemoDataContextAndItemSource.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:DemoDataContextAndItemSource"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<StackPanel Margin="15">
<WrapPanel>
<TextBlock Text="Window title: " />
<TextBox Text="{Binding Title, UpdateSourceTrigger=PropertyChanged}" Width="150" />
</WrapPanel>
<WrapPanel Margin="0,10,0,0">
<TextBlock Text="Window dimensions: " />
<TextBox Text="{Binding Width}" Width="50" />
<TextBlock Text=" x " />
<TextBox Text="{Binding Height}" Width="50" />
</WrapPanel>
</StackPanel>
</Window>
cs
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
}
Compile Success
Ex2:为什么 Width 和 Height 数是 NaN 并且编译失败?? (在 Xaml 中使用 DataContext)
<Window x:Class="DemoDataContextAndItemSource.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:DemoDataContextAndItemSource"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.DataContext>
<local:MainWindow/>
</Window.DataContext>
<StackPanel Margin="15">
<WrapPanel>
<TextBlock Text="Window title: " />
<TextBox Text="{Binding Title, UpdateSourceTrigger=PropertyChanged}" Width="150" />
</WrapPanel>
<WrapPanel Margin="0,10,0,0">
<TextBlock Text="Window dimensions: " />
<TextBox Text="{Binding Width}" Width="50" />
<TextBlock Text=" x " />
<TextBox Text="{Binding Height}" Width="50" />
</WrapPanel>
</StackPanel>
</Window>
Compile Failed
表达式
<Window.DataContext>
<local:MainWindow/>
</Window.DataContext>
将创建一个新的 MainWindow 实例并将其分配给 DataContext 属性。
为了分配当前实例,您必须编写
<Window ... DataContext="{Binding RelativeSource={RelativeSource Self}}">
我正在学习 WPF 中与 DataContext 的绑定方式,但我不明白为什么它会这样运行。 以下是代码。
Ex1:可以得到正确的绑定数据(在代码后面赋值DataContext)
Xaml
<Window x:Class="DemoDataContextAndItemSource.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:DemoDataContextAndItemSource"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<StackPanel Margin="15">
<WrapPanel>
<TextBlock Text="Window title: " />
<TextBox Text="{Binding Title, UpdateSourceTrigger=PropertyChanged}" Width="150" />
</WrapPanel>
<WrapPanel Margin="0,10,0,0">
<TextBlock Text="Window dimensions: " />
<TextBox Text="{Binding Width}" Width="50" />
<TextBlock Text=" x " />
<TextBox Text="{Binding Height}" Width="50" />
</WrapPanel>
</StackPanel>
</Window>
cs
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
}
Compile Success
Ex2:为什么 Width 和 Height 数是 NaN 并且编译失败?? (在 Xaml 中使用 DataContext)
<Window x:Class="DemoDataContextAndItemSource.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:DemoDataContextAndItemSource"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.DataContext>
<local:MainWindow/>
</Window.DataContext>
<StackPanel Margin="15">
<WrapPanel>
<TextBlock Text="Window title: " />
<TextBox Text="{Binding Title, UpdateSourceTrigger=PropertyChanged}" Width="150" />
</WrapPanel>
<WrapPanel Margin="0,10,0,0">
<TextBlock Text="Window dimensions: " />
<TextBox Text="{Binding Width}" Width="50" />
<TextBlock Text=" x " />
<TextBox Text="{Binding Height}" Width="50" />
</WrapPanel>
</StackPanel>
</Window>
Compile Failed
表达式
<Window.DataContext>
<local:MainWindow/>
</Window.DataContext>
将创建一个新的 MainWindow 实例并将其分配给 DataContext 属性。
为了分配当前实例,您必须编写
<Window ... DataContext="{Binding RelativeSource={RelativeSource Self}}">