textblock 相关问题

TextBlock是一个UI控件,用于在WPF(.NET Framework)中显示少量文本。

两个文本块接在一起

我有两个文本块,它们在页面上彼此相邻 - 姓名和姓氏。我将数据绑定到此,因此大小取决于它们的长度。让他们合作的最佳方式是什么

回答 2 投票 0

TextBlock 中的 WPF 格式日期时间?

我有一个绑定到 DateTime 属性的 TextBlock。如何配置日期的格式?

回答 4 投票 0

如何在网格视图中使用键盘(例如使用选项卡)关注 XAML WPF 中的 TextBlock?

我有一个文本块,它有一个带有转换器的绑定路径,但我无法使用选项卡按钮操作它。 Textblock 有一个 Onclick 属性,可以打开另一个页面。我想打开那个屏幕...

回答 1 投票 0

如何设置TextBlock中的制表符宽度?

我想减少 TextBlock 中制表符的宽度。制表符相当于大约 8 个字符。我想减少到大约 4 个空格。 TextBlock 文本块 = 新的 TextBloc...

回答 1 投票 0

TextBlock 中的 C# 超链接:单击它时没有任何反应

在我的 C# 独立应用程序中,我想让用户单击一个链接来启动他们最喜欢的浏览器。 System.Windows.Controls.TextBlock 文本 = new TextBlock(); Run run = new Run("链接 Tex...

回答 6 投票 0

在 WPF 中使用 RenderTargetBitmap 创建 TextBlock 的 png 图像时出现问题

当使用 RenderTargetBitmap 创建作为 Canvas 子级的 TextBlock 的 png 文件时,文本将拉伸到 TextBlock 的宽度。 TextBlock 作为“Right&qu...

回答 1 投票 0

设置 TextBlock 以保留开头和结尾的空白?

编辑: 下面的代码实际上按照我想要的方式工作 - 这个问题有点误导。请忽略它。 通常当我像这样设置 TextBlock 的 Text 属性时: TextBlock tb = new TextBlock(); TB...

回答 5 投票 0

文本框和文本块无法对齐

我目前正在练习xaml。我打算用文本框和文本块制作一个简单的菜单。 当我将文本块或文本框拖到画布上时,看起来不错。但我运行程序后却相差甚远...

回答 1 投票 0

在 wpf 文本块控件中换行时保留缩进

我使用属性 TextWrapping="Wrap" 设置了一个 WPF 文本块。 当我在开头传递一个带有制表符(在我的例子中为 vbTab)的长字符串时,我希望换行能够尊重...

回答 1 投票 0

WPF 绑定不适用于 TextBlock 中的前景属性

我有以下代码来设置前景属性 TextBlock 标签 = new TextBlock() { 样式 = LabelStyle }; 绑定绑定 = new Binding(LabelColor) { Source = dataContextProperty }; ...

回答 1 投票 0

为什么我的文本块的边框不是边框?

我刚刚在我的 XAML 文件中创建了这个 TextBlock: 我是

回答 1 投票 0

属性元素中不能同时包含文本和子元素“LineBreak”

我有以下 XAML: 我有以下 XAML: <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" TextAlignment="Center" TextWrapping="Wrap" Margin="15"> line1<LineBreak/><LineBreak/> line2 <TextBlock.Style> <Style TargetType="{x:Type TextBlock}"> <Setter Property="Visibility" Value="Hidden"/> <Style.Triggers> <DataTrigger Binding="{Binding State}" Value="ShowText"> <Setter Property="Visibility" Value="Visible"/> </DataTrigger> </Style.Triggers> </Style> </TextBlock.Style> </TextBlock> 但是 Visual Studio 抛出错误 Cannot have both the text 'line1' and the child element 'LineBreak' within a property element. 而且我不知道如何保留换行符并解决问题。我试过这个: <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" TextAlignment="Center" TextWrapping="Wrap" Margin="15"> <TextBlock.Text> line1<LineBreak/><LineBreak/> line2 </TextBlock.Text> <TextBlock.Style> <Style TargetType="{x:Type TextBlock}"> <Setter Property="Visibility" Value="Hidden"/> <Style.Triggers> <DataTrigger Binding="{Binding State}" Value="ShowText"> <Setter Property="Visibility" Value="Visible"/> </DataTrigger> </Style.Triggers> </Style> </TextBlock.Style> </TextBlock> 但是随后出现重复错误“文本属性设置多次”。 将内联放在样式后面: <TextBlock ...> <TextBlock.Style> ... </TextBlock.Style> line1<LineBreak/><LineBreak/> line2 </TextBlock>

回答 1 投票 0

在 ComboBox DataTemplate 中获取 TextBlock 的值

我有以下 XAML: 我有以下 XAML: <ComboBox Height="23" HorizontalAlignment="Left" Grid.Row="6" Grid.Column="2" Name="cbo_team" VerticalAlignment="Top" Width="148" DataContext="{Binding ElementName=cbo_component, Path=SelectedItem}" SelectedIndex="0"> <ComboBox.ItemsSource> <Binding XPath="Teams/Team/@id" Converter="{StaticResource xmlConverter}"> <Binding.ConverterParameter> <local:XmlConverterParameter XPathTemplate="/Products/Teams/Team[{0}]" XPathCondition="@id='{0}'" /> </Binding.ConverterParameter> </Binding> </ComboBox.ItemsSource> <ComboBox.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding XPath=@name}" /> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox> 在 C# 中,我试图获取 TextBlock 中当前选定项中的 ComboBox 的值。我怎么做? 这个问题差不多,但唯一的答案没有帮助。 检查这个样本。文本块(组合框下方)显示组合框中当前选定的 xml 元素的名称属性的值。将弹出一个消息框,其中包含在可视化树中查找的相同结果。初始选择更改时查找失败。看起来组合框项目是在设置所选项目后创建的。 XAML: <Window x:Class="CBTest.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Height="300" Width="300"> <Window.Resources> <XmlDataProvider x:Key="UsersData" XPath="Users"> <x:XData> <Users xmlns=""> <User name="Sally" /> <User name="Lucy" /> <User name="Linus" /> <User name="Charlie" /> </Users> </x:XData> </XmlDataProvider> </Window.Resources> <StackPanel> <ComboBox Name="_comboBox" ItemsSource="{Binding Source={StaticResource UsersData},XPath=*}" SelectedIndex="0" SelectionChanged="OnComboBoxSelectionChanged"> <ComboBox.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding XPath=@name}" Name="nameTextBlock" /> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox> <!-- Below shows how to get the value of selected item directly from the data. --> <TextBlock DataContext="{Binding Path=SelectedItem, ElementName=_comboBox}" Text="{Binding XPath=@name}" /> </StackPanel> </Window> 后面的代码,展示了如何通过遍历可视化树直接获取文本: private void OnComboBoxSelectionChanged(object sender, SelectionChangedEventArgs e) { ComboBox comboBox = sender as ComboBox; ComboBoxItem comboBoxItem = comboBox.ItemContainerGenerator.ContainerFromItem(comboBox.SelectedItem) as ComboBoxItem; if (comboBoxItem == null) { return; } TextBlock textBlock = FindVisualChildByName<TextBlock>(comboBoxItem, "nameTextBlock"); MessageBox.Show(textBlock.Text); } private static T FindVisualChildByName<T>(DependencyObject parent, string name) where T : DependencyObject { for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++) { var child = VisualTreeHelper.GetChild(parent, i); string controlName = child.GetValue(NameProperty) as string; if (controlName == name) { return child as T; } T result = FindVisualChildByName<T>(child, name); if (result != null) return result; } return null; } 抱歉来晚了一点 :) 但以下也有效(具有讽刺意味的是和你在同一个修复中!!) TextBlock tb1 = (TextBlock)cbo_team.SelectedItem; MessageBox.Show(tb1.Text); private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { ListBox ContactListBox = sender as ListBox; ListBoxItem listBoxItem = ContactListBox .ItemContainerGenerator.ContainerFromItem(ContactListBox.SelectedItem) as ListBoxItem; if (listBoxItem == null) { return; } TextBlock txtBlock = FindVisualChildByName<TextBlock>(listBoxItem, "ListTextBlock"); MessageBox.Show(txtBlock.Text); } private static T FindVisualChildByName<T>(DependencyObject parent, string name) where T : DependencyObject { for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++) { var child = VisualTreeHelper.GetChild(parent, i); string controlName = child.GetValue(NameProperty) as string; if (controlName == name) { return child as T; } T result = FindVisualChildByName<T>(child, name); if (result != null) return result; } return null; } 其他人已经建议使用 SelectionChanged 事件。没有测试下面的代码,但你可以试一试。 private void OnMyComboBoxChanged(object sender, SelectionChangedEventArgs e) { TextBlock tvContent = (sender as ComboBox).SelectedItem as TextBlock; string content = tvContent.Text; } private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { var comboBox = (ComboBox)sender; ComboBoxItem comboAsTextblock = (ComboBoxItem)comboBox.SelectedItem; string comboBoxItemText = comboAsTextblock.Content.ToString(); // comboBoxItemText is what you want :) }

回答 5 投票 0

如何更改包含在列表视图标题中的 TextBlock 的值?

我有一个 Listview,仅将标题设置为显示另一个列表的总计。如何访问 Textblock tbkValue 以更改其 Text 属性? 我有一个 Listview,仅将标题设置为显示来自另一个列表的总计。如何访问 Textblock tbkValue 以更改其 Text 属性? <ListView x:Name="lvwTotals" Margin="1,0,3,3" ScrollViewer.HorizontalScrollBarVisibility="Auto" > <ListView.ItemContainerStyle> <Style TargetType="ListViewItem"> <Setter Property="HorizontalContentAlignment" Value="Stretch" /> </Style> </ListView.ItemContainerStyle> <ListView.HeaderTemplate> <DataTemplate> <Grid Padding="12" Margin="3,5,1.5,5" Background="{ThemeResource SystemBaseLowColor}"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> <ColumnDefinition Width="30"/> <ColumnDefinition Width="30"/> <ColumnDefinition Width="30"/> <ColumnDefinition Width="30"/> <ColumnDefinition Width="70"/> <ColumnDefinition Width="70"/> </Grid.ColumnDefinitions> <TextBlock Grid.Column="0" x:Uid="PlayPgTbkTotals" TextWrapping="NoWrap" FontSize="12" /> <TextBlock Grid.Column="1" x:Name="tbkValue" TextWrapping="NoWrap" FontSize="12" /> <TextBlock Grid.Column="2" TextWrapping="NoWrap" FontSize="12" /> <TextBlock Grid.Column="3" TextWrapping="NoWrap" FontSize="12" /> <TextBlock Grid.Column="4" TextWrapping="NoWrap" FontSize="12" /> <TextBlock Grid.Column="5" TextWrapping="NoWrap" FontSize="12" /> <TextBlock Grid.Column="6" TextWrapping="NoWrap" FontSize="12" /> </Grid> </DataTemplate> </ListView.HeaderTemplate> </ListView> 一个简单的方法是使用 VisualTreeHelper 类。 代码隐藏: private void Button_Click(object sender, RoutedEventArgs e) { var textblcok = MyFindChildByName(lvwTotals, "tbkValue") as TextBlock; textblcok.Text = "New Content"; } public static DependencyObject MyFindChildByName(DependencyObject parant, string ControlName) { int count = VisualTreeHelper.GetChildrenCount(parant); for (int i = 0; i < count; i++) { var MyChild = VisualTreeHelper.GetChild(parant, i); if (MyChild is FrameworkElement && ((FrameworkElement)MyChild).Name == ControlName) return MyChild; var FindResult = MyFindChildByName(MyChild, ControlName); if (FindResult != null) return FindResult; } return null; }

回答 1 投票 0

在 WPF 中,如果前面有一些文本,长单词不会在第一行换行

我在 WPF 的 TextBox 中包装一些文本时遇到问题。 假设我有以下文本“这是一些 VeryLongTextThatHasToBeWrapped 等等”。 我在 WPF 的 TextBox 中包装一些文本时遇到问题。 假设我有以下文字“这是一些 VeryLongTextThatHasToBeWrapped 等等”。 <TextBox Text="Here is some VeryLongTextThatHasToBeWrapped and so on" TextWrapping="Wrap" MaxWidth="70" Height="150"/> 我希望文本会在第一行换行,因为它不适合TextBox。喜欢这里: 但是换行发生在下一行,所以我有未使用的空间并且整个文本不适合 TextBlock。喜欢这里: 我尝试玩Width 和Height,尝试将TextWrapping设置为WrapWithOverflow. 另外,我玩了 Run 元素和 InlineUIContainer 没有运气。 还是得不到想要的结果 有没有什么办法可以“强制”换行尽快打断一个词,而不是给它最大可能的空间然后再打断它? 您可以将字符串视为字符数组并将其呈现在ItemsControl中,您将ItemsPanel设置为WrapPanel,当然它不可编辑。 <ItemsControl ItemsSource="Here is some VeryLongTextThatHasToBeWrapped and so on" MaxWidth="70"> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <WrapPanel/> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> </ItemsControl>

回答 1 投票 0

动态更新文本块值

大家好我正在尝试通过 TCP/IP 读取值来动态更新文本块。我做错了什么,我该如何解决?另外,如果有更好的做事方式,我愿意学习……

回答 0 投票 0

在WPF中,当文字太长时,带滑块的TextBlock。

如标题。我想创建TextBlock的水平和垂直滑块,将根据文本大小自动调整。谷歌只是显示我的滑块控件,这绝对不是......

回答 2 投票 -1

如何在WPF平台中获得控件的实际宽度和实际高度

[我正在WPF平台中准备一个自定义控件,在该控件中,我正在使用内容演示者来显示视图,并且尝试使用文本在内容演示者内容上显示文本...

回答 1 投票 0

默认的TextBlock样式覆盖按钮的文本颜色

我的问题出现在.NET 3.5 SP1中的WPF中,可以描述如下:我的默认样式会击中UI中的所有TextBlock元素。看起来就是这样:

回答 3 投票 6

将原始json绑定到WPF文本块

请帮助我将来自REST API的原始json数据绑定到任何WPF控件。我以为WPF文本块。这是隐藏的代码:try {...

回答 1 投票 -1

© www.soinside.com 2019 - 2024. All rights reserved.