我的XAML中有一个TextBlock,它的文本绑定到我的viewmodel中的属性.
<TextBlock x:Name="SomeText" Text="{Binding TheTextProperty}" />
Run Code Online (Sandbox Code Playgroud)
这工作正常,但在设计时,没有视图模型,因此该属性不可解析且文本为空.这在设计器中很难处理,因为它没有显示可见文本.
如何在设计时指定一些默认文本?
我有一个TextBlock,就像这样:
<ScrollViewer HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto">
<TextBlock Name="PatchNotesTxt" Width="291" Height="226" Style="{StaticResource PatchNotes}">
Test Test Test Test Test Test Test Test Test Test Test Test Test Test TEST
</TextBlock>
</ScrollViewer>
Run Code Online (Sandbox Code Playgroud)
但问题是,当内容到达TextBlock的末尾时,它不会转移到新行.我该怎么做到这一点?
我收到错误:
WinRT信息:无法解析指定对象上的TargetProperty Canvas.Left.
运行时出现此错误(构建正常).代码的重要部分:
var canvasAnim = new DoubleAnimation() { To = -50, Duration = new Duration(TimeSpan.FromSeconds(0.3)) };
Storyboard.SetTarget(canvasAnim, MyWipedText);
Storyboard.SetTargetProperty(canvasAnim, "Canvas.Left");
Run Code Online (Sandbox Code Playgroud)
我在代码后面附加动画,因为这是在对更大的控件进行一般滑动之后完成的.知道如何让这个属性工作?(不透明度在这里工作正常顺便说一句).
谢谢
我需要一个具有 TextTrimming 和 MaxLines 的 TextBlock 或 TextBox。TextBlock 有一个 TextTrimming 属性,TextBox 有一个 MaxLines 属性,但两者都不支持。这是一个更大的控件的一部分,允许用户输入笔记并将它们显示在列表框中(请参阅: Grief with WPF UserControl with ListBox and TextBox Can't get textwrapping to work如果有兴趣)。基本上,对于已经输入的笔记,我想将笔记限制为 2 行。如果它长于 2 行,我会在它的末尾显示一个椭圆 (...) 以表示注释还有更多内容。如果用户将鼠标悬停在缩写的笔记上,他/她会看到整个笔记作为弹出窗口。* 该控件将放置在主网格上,并且可能具有不同的大小(或调整大小),因此 2 行中的文本数量可能会发生变化. 即在某些情况下可以完全显示的注释在其他情况下可能会被缩写。
尝试将 MaxLines 功能添加到 TextBlock 或将 TextTrimming 添加到 TextBox 会更好吗?我看到一些帖子似乎与我需要做的很接近。这是一个将 TextTrimming 添加到 TextBox 但仅在编辑时并且不清楚 MaxLines 是否仍然有效的方法: TextBox TextTrimming 我没有遇到任何关于将 MaxLines 属性添加到 TextBlock 的问题。请注意,Windows Phone 的 TextBlock 似乎有它。也许我不是唯一需要这个的人:)
我有点惊讶这不是“开箱即用”的。这似乎是一个普遍的问题。顺便说一句,对于 TextBox、TextBlock,甚至 Label 或其他东西,都没有偏好。这些只是列表框项目,不可编辑。
任何想法或指针都非常感谢。
-Dave *如果您在想,“当用户将鼠标悬停在缩写注释上时,他接下来可能会询问如何显示弹出窗口”,您说得非常正确!
这是一种基于此 stackoverflow 帖子处理该问题的方法:可 滚动的 TextBlock 大小正好为 2 …
所以我希望改变我的程序,以便我可以运行一个函数来检查前景色是否应该是黑色或银色。我希望将“不可访问”的字段涂灰。
我的表格目前看起来像:
我希望将“无需维护”字段“变灰”。但是我在尝试为数据模板中的字体前景定义绑定元素时遇到问题。
我已经尝试了从尝试在主窗口代码中定义 IValueConverter 类到定义窗口键资源的所有方法,但似乎我无法在文本块元素本身的数据模板中执行此操作?
任何建议/帮助将不胜感激。谢谢!
XAML:
<Grid Margin="0,0,2,0">
<ListBox x:Name="allSites_LB"
HorizontalAlignment="Left"
Height="400"
Margin="20,60,0,0"
VerticalAlignment="Top"
Width="945"
Loaded="allSites_LB_Loaded"
BorderThickness="1" SelectionChanged="allSites_LB_SelectionChanged"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
>
<ListBox.ItemTemplate >
<DataTemplate >
<Border BorderBrush="Black" BorderThickness="0,0,0,1" Margin="-20,1,0,1" Padding="0,5,0,5" >
<Grid Margin="75,3" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200" />
<ColumnDefinition Width="400" />
<ColumnDefinition Width="345" />
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding SiteNo}" Grid.Column="0" FontSize="16" />
<TextBlock Text="{Binding Address}" Grid.Column="1" FontSize="16" Margin="50,1" />
<TextBlock Text="{Binding MaintStatus}" Grid.Column="2" FontSize="16" />
</Grid>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button x:Name="viewHistory_BTN"
Content="View History"
HorizontalAlignment="Left"
Height="52"
Margin="20,496,0,0"
VerticalAlignment="Top"
Width="172" FontSize="20" …
Run Code Online (Sandbox Code Playgroud) 我需要将 TextBlock 代码后面的文本设置为包含格式化文本的字符串。
例如这个字符串:
"This is a <Bold>message</Bold> with bold formatted text"
Run Code Online (Sandbox Code Playgroud)
如果我以这种方式将此文本放入 xaml 文件中,它可以正常工作
<TextBlock>
This is a <Bold>message</Bold> with bold formatted text
</TextBlock>
Run Code Online (Sandbox Code Playgroud)
但如果我使用 Text 属性设置它,则不起作用。
string myString = "This is a <Bold>message</Bold> with bold formatted text";
myTextBlock.Text = myString;
Run Code Online (Sandbox Code Playgroud)
我知道我可以使用内联:
myTextBlock.Inlines.Add("This is a");
myTextBlock.Inlines.Add(new Run("message") { FontWeight = FontWeights.Bold });
myTextBlock.Inlines.Add("with bold formatted text");
Run Code Online (Sandbox Code Playgroud)
但问题是我从另一个来源获取字符串,但我不知道如何将此字符串传递到 TextBlock 并查看是否已格式化。我希望有一种方法可以直接使用格式化字符串设置 TextBlock 的内容,因为我不知道如何解析该字符串以将其与 Inlines 一起使用。
我正在尝试将一些SystemColor前景分配给XAML上的TextBlock for WPF.主要的想法是让应用程序更容易访问,即使用高对比度也可以很好地看到它.
我尝试了以下代码,但它对显示的颜色没有任何影响:
<StackPanel Orientation="Horizontal"
Background="{DynamicResource {x:Static SystemColors.HotTrackBrush}}">
<TextBlock Text="my app"
HorizontalAlignment="Left"
Style="{StaticResource MainPageText}"
VerticalAlignment="Center"
TextWrapping="Wrap"
x:Name="Title"/>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)
我对此很新,所以你能告诉我你认为的问题吗?
提前致谢.
在这个问题(来自eandersson的答案)中,在a中使用了一个超链接TextBlock
.我想做同样但在后面的代码 - 如何做到这一点?
链接示例:
<TextBlock>
<Hyperlink NavigateUri="http://www.google.com" RequestNavigate="Hyperlink_RequestNavigate">
Click here
</Hyperlink>
</TextBlock>
private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
{
Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
e.Handled = true;
}
Run Code Online (Sandbox Code Playgroud) 关于如何仅绑定部分'TextBlock.Text'字符串的任何想法.
例如:
我想添加"你的名字是{Binding Path = Name}"并拥有'OneWay Binding',而不必使用另一个'TextBlock'控件.
如果我的文件名和路径不适合,我想显示省略号TextBlock
。
例如,如果我的文件名和路径如下:
C:\examples\example\folderA\folderB\folderC\myfilename.txt
Run Code Online (Sandbox Code Playgroud)
我想这样显示TextBlock
:
C:\examples...myfilename.txt
Run Code Online (Sandbox Code Playgroud)
更新:
我知道我可以TextTrimming
在最后设置省略号。但是我需要一种在中间的中间设置省略号的方法。
textblock ×10
wpf ×6
c# ×4
xaml ×3
animation ×1
binding ×1
data-binding ×1
ellipsis ×1
line ×1
listbox ×1
mvvm ×1
new-operator ×1
partial ×1
silverlight ×1
storyboard ×1
styles ×1
systemcolors ×1
text ×1
textbox ×1