UserControl中的TextBlock不显示文本

时间:2018-06-25 12:48:53

标签: c# wpf user-controls border textblock

我正在尝试在C#/ WPF中创建一个Nonogram(又名PuzzleCross)拼图网格,并创建了两个UserControls来包含行键和列键。每个UserControl都包含一个Border,该Border包含一个TextBlock和一个名为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);
        }
    }

1 个答案:

答案 0 :(得分:3)

类似的绑定

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

仅在您将x:Name属性分配给UserControl时有效:

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

但是,相对于RelativeSource绑定是不必要的:

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