嗨,我想掌握绑定(bind)的窍门。
XAML 代码:
<Window x:Class="WPF_SandBox.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">
<StackPanel x:Name="stackPanel">
<TextBox x:Name="textBox_FirstName" Width="200" Margin="0,100,0,0" Text="{Binding Path=FirstName, UpdateSourceTrigger=PropertyChanged}" />
<TextBox x:Name="textBox_LastName" Width="200" Margin="0,10,0,0" Text="{Binding Path=LastName, UpdateSourceTrigger=PropertyChanged}" />
<TextBlock x:Name="textBlock_FullName" Background="LightBlue" Width="200" Margin="0,10,0,0" Text="{Binding Path=FullName, UpdateSourceTrigger=PropertyChanged}" />
</StackPanel>
</Window>
C# 代码:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Person person = new Person { FirstName = "Matt", LastName = "Smith" };
stackPanel.DataContext = person;
}
}
public class Person : INotifyPropertyChanged
{
string firstName;
string lastName;
public string FirstName
{
get
{
return firstName;
}
set
{
firstName = value;
OnPropertyChanged("FirstName");
OnPropertyChanged("FullName");
}
}
public string LastName
{
get { return lastName; }
set
{
lastName = value;
OnPropertyChanged("LastName");
OnPropertyChanged("FullName");
}
}
public string FullName
{
get
{
return String.Format("{0}, {1}",lastName,firstName);
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
}
启动时,会显示一个带有 2 个文本框和 1 个文本 block 的窗口。在窗口构造函数中,我创建了一个 person 实例并将 stackPanel 的 DataContext 分配给该实例。第一个文本框绑定(bind)到 Person 类的 FirstName 属性,第二个 TextBox 绑定(bind)到 LastName 属性,最后的 TextBlock 只打印 LastName 属性和 FirstName 属性。如前所述,我在 C# 代码中设置了 stackPanel 的 DataContext。如何在 XAML 中设置它?例如:
<StackPanel x:Name="stackPanel" DataContext="person">
<TextBox x:Name="textBox_FirstName" Width="200" Margin="0,100,0,0" Text="{Binding Path=FirstName, UpdateSourceTrigger=PropertyChanged}" />
<TextBox x:Name="textBox_LastName" Width="200" Margin="0,10,0,0" Text="{Binding Path=LastName, UpdateSourceTrigger=PropertyChanged}" />
<TextBlock x:Name="textBlock_FullName" Background="LightBlue" Width="200" Margin="0,10,0,0" Text="{Binding Path=FullName, UpdateSourceTrigger=PropertyChanged}" />
</StackPanel>
这不起作用,但正如您所见,我正在尝试在 XAML 中设置 stackPanel 的 DataContext,我该怎么做?
谢谢!
最佳答案
This brief article解释了设置 DataContext 的两种方法:通过 XAML 或通过代码。这是代码:
public class HelloWorldDataContextModel
{
public string HelloWorld { get; set; }
public HelloWorldDataContextModel()
{
HelloWorld = "Hello world!";
}
}
和 XAML:
<Window x:Class="HelloWorldDataContext.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:HelloWorldDataContext"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:HelloWorldDataContextModel x:Key="HelloWorldDataContext" />
</Window.Resources>
<Grid DataContext="{StaticResource HelloWorldDataContext}">
<TextBox HorizontalAlignment="Left" Height="23" Margin="222,127,0,0" TextWrapping="Wrap" Text="{Binding HelloWorld}" VerticalAlignment="Top" Width="120"/>
</Grid>
</Window>
关于wpf - 在 XAML 中设置元素的 DataContext?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25267070/