我在WPF中创建了一个用户控件:
<UserControl x:Class="TestUserControl.Controls.GetLatest"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<TextBlock Name="theTextBlock"/>
</UserControl>
代码后台有一个名为“FirstMessage”的参数,它将其设置为我的用户控件TextBlock的文本:
public partial class GetLatest : UserControl
{
public string FirstMessage { get; set; }
public GetLatest()
{
InitializeComponent();
theTextBlock.Text = this.FirstMessage;
}
}
在我的主代码中,我可以通过智能感知设置用户控件中的FirstMessage参数:
<Window x:Class="TestUserControl.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300"
xmlns:controls="clr-namespace:TestUserControl.Controls"
>
<StackPanel>
<controls:GetLatest FirstMessage="This is the title"/>
</StackPanel>
</Window>
然而,它仍然没有设置文本。我尝试过Text="{Binding Path=FirstMessage}"和其他语法,但都没有起作用。
我该如何在我的用户控件中访问FirstMessage的值?
- Chad