我有一個用戶控件,看起來像這樣的代碼:綁定的字符串屬性給TextBlock
using System;
using System.Windows;
using System.Windows.Controls;
namespace Client
{
public partial class Spectrum : UserControl
{
public string AntennaName { get; set; }
public Spectrum()
{
InitializeComponent();
}
}
}
和XAML(不是全部,但重要的部分):
<UserControl x:Class="Client.Spectrum"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Client"
mc:Ignorable="d" d:DesignHeight="150" d:DesignWidth="350"
Background="#253121" BorderBrush="Black" BorderThickness="1"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<StackPanel
<TextBlock Margin="10,3,0,0" Foreground="White"
FontWeight="Bold" FontStyle="Italic" TextAlignment="Left"
Text="{Binding AntennaName}"/>
</StackPanel>
</UserControl>
你可以看到即時嘗試將AntennaName屬性綁定到TextBlock.Text屬性,但沒有多少運氣。 你能告訴我我做錯了什麼?
您有什麼問題? – SLaks
TextBlock.Text不會根據AntennaName更改而更改 –
「Spectrum」類的實例是否設置爲View的DataContext? – sll