UserControl 中的 TextBlock 不显示文本

TextBlock in UserControl not displaying text

我正在尝试在 C#/WPF 中创建一个 Nonogram(又名 PuzzleCross)拼图网格,并创建了两个 UserControls 来包含行键和列键。每个 UserControl 都包含一个包含 TextBlock 的 Border,以及一个名为 TextControl 的 DependencyProperty,使 Text 属性 可以在 UserControl 之外访问。除了在 运行 时文本没有实际显示外,一切正常。 TextControl 包含正确的文本,正如使用 MouseDown 事件和 MessageBox 测试的那样,但由于某种原因文本不存在。

谁能帮我弄清楚我错过了什么?我觉得这是一件简单的事情,但我只是没有看到它。

水平用户控件:

    <Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Center" Height="10" Width="100">
        <TextBlock Text="{Binding ElementName=HorizontalRowLabel, Path=TextContent}" Foreground="Black" FontSize="6" MouseDown="TextBlock_MouseDown"/>
    </Border>

水平 C#:

public partial class HorizontalRowLabel : UserControl
{
    public static readonly DependencyProperty TextContentProperty = DependencyProperty.Register("TextContent", typeof(string),
        typeof(HorizontalRowLabel), new FrameworkPropertyMetadata(""));
    public string TextContent
    {
        get { return (string)GetValue(TextContentProperty); }
        set { SetValue(TextContentProperty, value); }
    }

    private void TextBlock_MouseDown(object sender, MouseButtonEventArgs e)
    {
        MessageBox.Show(TextContent);
    }

    public HorizontalRowLabel()
    {
        InitializeComponent();
    }
}

    //Adds text HorizontalRowLabel UserControl, then adds HRL to Grid.
    public void InitRowKeys(Grid puzzle)
    {
        for(int i = 0; i < HorizontalKeys.Length; i++)
        {
            RowDefinition row = new RowDefinition();
            HorizontalRowLabel hrow = new HorizontalRowLabel();

            row.Height = new GridLength(10);

            for(int j = 0; j < HorizontalKeys[i].Length; j++)
            {
                if(HorizontalKeys[i].Length == 0 || j == HorizontalKeys[i].Length - 1)
                {
                    hrow.TextContent += HorizontalKeys[i][j].ToString();
                    hrow.Foreground = Brushes.Black;
                    hrow.SetValue(Grid.RowProperty, i);
                    hrow.SetValue(Grid.ColumnProperty, 0);
                    hrow.FontSize = 6;
                    hrow.HorizontalAlignment = HorizontalAlignment.Right;
                    hrow.VerticalAlignment = VerticalAlignment.Center;

                }
                else
                {
                    hrow.TextContent += HorizontalKeys[i][j].ToString() + " ";
                    hrow.SetValue(Grid.RowProperty, i);
                    hrow.SetValue(Grid.ColumnProperty, 0);
                    hrow.FontSize = 6;
                    hrow.HorizontalAlignment = HorizontalAlignment.Right;
                    hrow.VerticalAlignment = VerticalAlignment.Center;
                }
            }
            //puzzle.Margin = new Thickness(0,50,0,0);
            hrow.Width = 100;
            hrow.Height = 30;


            puzzle.RowDefinitions.Add(row);
            puzzle.Children.Add(hrow);
        }
    }

像这样的绑定

Text="{Binding ElementName=HorizontalRowLabel, Path=TextContent}"

仅当您已将 x:Name 属性分配给 UserControl 时才有效:

<UserControl ... x:Name="HorizontalRowLabel">
    ...
</UserControl>

然而,对于 RelativeSource 绑定则没有必要:

Text="{Binding TextContent, RelativeSource={RelativeSource AncestorType=UserControl}}"