在WPF中,如果我在网格中放置任何控件,如果我调整网格大小,它会自动调整其中的所有控件的大小.但是在标签或文本块或任何其他文本元素中,所有控件大小都会改变但字体大小保持不变,它不会改变.
如果字体必须按网格大小更改,应该怎么做?
如何在WPF/Silverlight中将TextBlock添加到Ellipse?
如果我定义了以下样式:
<UserControl.Resources>
<Style TargetType="TextBlock" x:Key="ProblemStyle">
<Setter Property="FontSize" Value="40"/>
<Setter Property="FontWeight" Value="Bold"/>
</Style>
</UserControl.Resources>
Run Code Online (Sandbox Code Playgroud)
然后,当我将ContentPresenter数据绑定到字符串时,在WPF中我可以使用以下XAML根据需要设置文本样式:
<ContentPresenter Content="{Binding Problem}">
<ContentPresenter.Resources>
<Style TargetType="TextBlock" BasedOn="{StaticResource ProblemStyle}" />
</ContentPresenter.Resources>
</ContentPresenter>
Run Code Online (Sandbox Code Playgroud)
但是,在Silverlight中,这不起作用.有没有办法适用于两者?
我有一个绑定到数据库查询结果的列表框.我正在使用一个项目模板,在一行上显示主题,我希望它在另一行显示正文的预览.我想知道的是 - 显然身体太长了以至于无法适应那里,我能以某种方式将其设置为仅显示第一个如此多的字符并在其后添加省略号,就像预览一样吗?或者甚至一些接近的东西也没关系.例如:
而不是显示:
Lorem ipsum dolor坐下来,精致的adipistur elit.Suspendisse vitae eros nibh.Pellentesque居民morbi tristique senectus et netus et malesuada fames ac turpis egestas.Donec augue metus,iaculis id porta non,pellentesque quis turpis.Donec rutrum diam eget tortor bibendum vel blandit odio iaculis.Curabitur pretium adipiscing orci,ut pulvinar justo vehicula non.Mauris nec ipsum velit.Sed et auctor nibh.Proin ac ultricies tellus.
它会显示类似的东西
Lorem ipsum dolor坐下来,精致的adipistur elit.Suspendisse ...
有任何想法吗?
我想从dependencyproperty更改TextBlock的前景色.
但我不改变文本块颜色.
我的代码中不知道这个问题.
如何使用触发器更改TextBlock的前景色?
XAML:
<TextBlock Name="TestBlock" Text="Test color" >
<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
<Trigger Property ="IsMouseOver" Value="True">
<Setter Property= "Foreground" Value="Gray"/>
</Trigger>
<DataTrigger Binding="{Binding Path=TestColorMode2, RelativeSource={RelativeSource AncestorType={x:Type local:Window1}}}" Value="0">
<Setter Property="Foreground" Value="Red" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=TestColorMode2, RelativeSource={RelativeSource AncestorType={x:Type local:Window1}}}" Value="1">
<Setter Property="Foreground" Value="Blue" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=TestColorMode2, RelativeSource={RelativeSource AncestorType={x:Type local:Window1}}}" Value="2">
<Setter Property="Foreground" Value="Green" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=TestColorMode2, RelativeSource={RelativeSource AncestorType={x:Type local:Window1}}}" Value="3">
<Setter Property="Foreground" Value="Black" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
Run Code Online (Sandbox Code Playgroud)
码:
public static DependencyProperty TestColorModeProperty …
Run Code Online (Sandbox Code Playgroud) 我的XAML中有一个属性绑定到我的ViewModel上的属性(实现).一个单独的按钮启动一个长时间运行(2-3秒)的过程,如下所示:please wait
TextBlock
Visibility
IsBusy
INotifyPropertyChanged
IsBusy = true;
try
{
// Do some long-running process
}
finally
{
IsBusy = false;
}
Run Code Online (Sandbox Code Playgroud)
但这条please wait
消息从未出现过.我假设操作在UI线程上运行,因此没有给它刷新的机会?如果我在一个单独的线程上运行上面的代码,它确实有效,但我不希望用户在操作运行时做任何事情 - 我很高兴用户界面freeze
.
如何显示please wait
消息?或者我会更好地在后台线程上运行它并(以某种方式)锁定UI持续时间?我正在使用.Net 4顺便说一句.
好的...所以这个解决方案没有帮助
XAML就在这里
<ListBox ItemsSource="{Binding Path=ContentItems}" Background="White" >
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Margin="2" Height="Auto" Width="Auto">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid Grid.Column="0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Orientation="Horizontal">
<Label VerticalAlignment="Center" Margin="0,0,0,5">Start</Label><svl:TimeEditor Value="{Binding Path=FormatedStart}" Width="87" HorizontalAlignment="Left" Margin="2,8" Name="dtpStart" FontSize="12" Height="25" VerticalAlignment="Center" />
<Label VerticalAlignment="Center" Margin="0,0,0,5">End</Label><svl:TimeEditor Value="{Binding Path=FormatedEnd}" Width="87" HorizontalAlignment="Left" Margin="2,8" Name="dtpEnd" FontSize="12" Height="25" VerticalAlignment="Center" />
</StackPanel>
<TextBlock Grid.Row="1" TextWrapping="Wrap" Name="tbText" Text="{Binding Path=Data}"></TextBlock>
</Grid>
<Grid Grid.Column="1" Visibility="Collapsed">
</Grid>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Run Code Online (Sandbox Code Playgroud) 我有一个像这样的flowdocument:
var mcFlowDoc = new FlowDocument();
var para = new Paragraph();
para.Inlines.Add(textBlock1);
para.Inlines.Add(textBlock2);
para.Inlines.Add(textBlock3);
mcFlowDoc.Blocks.Add(para);
richTextBox1.Document = mcFlowDoc;
Run Code Online (Sandbox Code Playgroud)
我需要一个事件来触发鼠标点击文本块:
<RichTextBox Margin="10,10,230,12" Name="richTextBox1" FontFamily="Simplified Arabic" FontSize="16" IsReadOnly="True" IsReadOnlyCaretVisible="False" ForceCursor="False" FlowDirection="RightToLeft" VerticalScrollBarVisibility="Auto">
<RichTextBox.Resources>
<Style TargetType="Run">
<EventSetter Event="MouseLeftButtonDown" Handler="Run_Click" />
</Style>
<Style TargetType="TextBlock">
<EventSetter Event="MouseLeftButtonDown" Handler="TextBlock_Click" />
</Style>
</RichTextBox.Resources>
</RichTextBox>
void TextBlock_Click(object sender, MouseButtonEventArgs e)
{
TextBlock tb = sender as TextBlock;
}
Run Code Online (Sandbox Code Playgroud)
Run的事件处理程序被调用并且正常工作(在flowdocument中更改内联),但TextBlock 的事件处理程序不是.
我究竟做错了什么?谢谢
几个月前我提出了类似的问题
在这种情况下,我有一个像这样的文本块
<TextBlock FontSize="28" Text="{Binding DataPoint.Y, StringFormat=\{0:0\\%\}}" Foreground="Black">
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,我的 StringFormat 在数字后面放置了一个“%”符号,如果我的数据是 0.0(我在后面的代码中填充了组件,我的变量是双精度)我得到“0%”
但现在如果我的文本是 0.0 我想得到“”
到目前为止我有这个:
Text="{Binding DataPoint.Y, StringFormat=\{0:#.#\\%\}}"
Run Code Online (Sandbox Code Playgroud)
但这检索到“%”,我怎样才能得到“”?
案例非常简单:FontWeight的属性TextBlock
在Universal Store App中不起作用.我已经创建了示例项目(Universal App),并在WindowPhone中MainPage
添加了TextBlock
样式:
<TextBlock HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize=20
FontWeight="Thin"
Text="Test text should be thin in runtime." />
Run Code Online (Sandbox Code Playgroud)
在设计器中,一切看起来都很好,但是当我在模拟器/设备上部署应用程序时,测试文本不会变薄!这是正常的.看看截图:
正如您所看到的,在设计师中,文字非常薄(它很漂亮!).但是在运行模拟器中,文本的重量是正常的.为什么?我该如何解决它?在此先感谢您的帮助.
textblock ×10
wpf ×9
c# ×4
silverlight ×2
xaml ×2
datatrigger ×1
ellipse ×1
foreground ×1
label ×1
resize ×1
richtextbox ×1
styles ×1
truncate ×1
visibility ×1
word-wrap ×1