代码之家  ›  专栏  ›  技术社区  ›  Keven M

用户控件中的文本块不显示文本

  •  1
  • Keven M  · 技术社区  · 6 年前

    我试图在C/WPF中创建一个自动生成的(也称为自动生成)的难题网格,并创建了两个用于包含行和列键的索引器。每个USER控件都包含一个包含文本块的边框,一个名为Text控件的依赖性属性使文本属性在USER控件之外访问。一切正常,除了运行时文本实际上不显示。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 回复  |  直到 6 年前
        1
  •  3
  •   Clemens    6 年前

    捆绑式

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

    只有在您分配了 x:Name 用户控件的属性:

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

    但是,对于相对资源绑定,这是不必要的:

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