标签: textblock

设置WPF的Textblock.Text属性会抛出NullReferenceException

textBlock.Text = "Text";
Run Code Online (Sandbox Code Playgroud)

这是我的代码,它没有显示任何错误.但是当我运行它时,我得到了一个NullReferenceException

Object reference not set to an instance of an object.
Run Code Online (Sandbox Code Playgroud)

这个语句在Slider的ValueChanged事件中,如果重要的话.

c# wpf text textblock

0
推荐指数
1
解决办法
6835
查看次数

如何将WPF TextBlock绑定到右上角?

请帮我编辑XAML,以便TextBlock显示分钟到右上角.

<StackPanel Orientation="Horizontal" >
   <StackPanel Orientation="Horizontal" >
      <TextBlock Name="UserNameTextBlock"  Margin="0,0,8,0"  VerticalAlignment="Bottom" FontSize="15" Text="{Binding Path=UserName}" FontWeight="Bold"></TextBlock>
      <TextBlock   FontSize="13" VerticalAlignment="Bottom" Padding="0,0,0,1" Foreground="LightGray" >@</TextBlock>
      <TextBlock  FontSize="13"  VerticalAlignment="Bottom" Padding="0,0,0,1" Name="ScreenNameTextBlock"  Text="{Binding Path=ScreenName}" Foreground="Gray" ></TextBlock>
      <TextBlock  FontSize="13"  VerticalAlignment="Bottom" Padding="0,0,0,1" Name="MinAgo"  Text="{Binding Path=MinAgo}" Foreground="Gray" ></TextBlock>    
     </StackPanel>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)

所以应该是这样的

在此输入图像描述

wpf xaml textblock

0
推荐指数
1
解决办法
2120
查看次数

当 TextBlock 是 UserControl 的一部分时,如何处理 TextBlock.KeyDown 事件?

我有这个简单的UserControl

<UserControl x:Class="WPFTreeViewEditing.UserControl1"
             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" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <TextBlock Text="Hello, world!" KeyDown="TextBlock_KeyDown" />
    </Grid>
</UserControl>
Run Code Online (Sandbox Code Playgroud)

我想处理 TextBlock.KeyDown 事件。因此,我在代码隐藏中添加了一个事件处理程序:

public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();
    }

    private void TextBlock_KeyDown(object sender, KeyEventArgs e)
    {
        MessageBox.Show("Key up!");
    }
}
Run Code Online (Sandbox Code Playgroud)

但它不着火。怎么了?

更新。

PreviewKeyDown也不火。

UserControl用于HierarchicalDataTemplate当时:

<Window x:Class="WPFTreeViewEditing.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WPFTreeViewEditing"
        Title="MainWindow" Height="265" Width="419">
    <Grid>
        <TreeView ItemsSource="{Binding}">
            <TreeView.Resources>
                <HierarchicalDataTemplate DataType="{x:Type local:ViewModel}" ItemsSource="{Binding Items}">
                    <local:UserControl1 />
                </HierarchicalDataTemplate>
            </TreeView.Resources>
        </TreeView>
    </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

c# wpf textblock

0
推荐指数
1
解决办法
2788
查看次数

如何在WPF TextBlock中强制文本换行

我使用TextBlock:

<TextBlock Text="HelloWorld" Width="600" />
Run Code Online (Sandbox Code Playgroud)

如何让我的文本在两行上呈现:

Hello
World
Run Code Online (Sandbox Code Playgroud)

我可以使用特殊的换行符,如下所示吗?

<TextBlock Text="Hello\nWorld" Width="600" />
Run Code Online (Sandbox Code Playgroud)

我不想改变TextBlock的宽度,因为我不想使用自动文本换行算法,如TextWrapping ="Wrap"或TextWrapping ="WrapWithOverflow":我希望能够在字符串本身指定它应该换行的位置.

wpf textblock

0
推荐指数
1
解决办法
1808
查看次数

如何从WPF中将TextBlock转换为c#中的字符串?

如何TextBlock从wpf中将c#转换为c#中的字符串?我有一个包含文本块的列表框,以便更改前景色.但是当我得到listbox.selecteditem时,它显然会返回一个文本块,但我需要知道文本块是什么,转换成字符串.我怎么做?

c# wpf textblock

-4
推荐指数
1
解决办法
2082
查看次数

标签 统计

textblock ×5

wpf ×5

c# ×3

text ×1

xaml ×1