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事件中,如果重要的话.
请帮我编辑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)
所以应该是这样的
我有这个简单的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) 我使用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":我希望能够在字符串本身指定它应该换行的位置.
如何TextBlock
从wpf中将c#转换为c#中的字符串?我有一个包含文本块的列表框,以便更改前景色.但是当我得到listbox.selecteditem时,它显然会返回一个文本块,但我需要知道文本块是什么,转换成字符串.我怎么做?