根据Windows应用程序开发与Microsoft .NET 4 70-511培训工具包
Label控件和TextBlock控件有什么区别,因为它们都是内容控件而只是显示文本?
我正在做一些基准测试,以确定我是否可以将WPF用于新产品.然而,早期的表现结果令人失望.我做了一个快速的应用程序,它使用数据绑定每100毫秒在列表框内显示一堆随机文本,它占用了大约15%的CPU.所以我创建了另一个跳过数据绑定/数据模板方案的快速应用程序,除了每100毫秒更新一个ListBox内的10个TextBlocks之外什么都不做(实际产品不需要100毫秒更新,更像是500毫秒最大值,但是这是一个压力测试).我仍然看到大约5-10%的CPU使用率.为什么这么高?是因为所有的垃圾串吗?
这是不使用绑定的版本的XAML:
<Grid>
<ListBox x:Name="numericsListBox">
<ListBox.Resources>
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="48"/>
<Setter Property="Width" Value="300"/>
</Style>
</ListBox.Resources>
<TextBlock/>
<TextBlock/>
<TextBlock/>
<TextBlock/>
<TextBlock/>
<TextBlock/>
<TextBlock/>
<TextBlock/>
<TextBlock/>
<TextBlock/>
</ListBox>
</Grid>
Run Code Online (Sandbox Code Playgroud)
这是背后的代码:
public partial class Window1 : Window
{
private int _count = 0;
public Window1()
{
InitializeComponent();
}
private void OnLoad(object sender, RoutedEventArgs e)
{
var t = new DispatcherTimer(TimeSpan.FromSeconds(0.1), DispatcherPriority.Normal, UpdateNumerics, Dispatcher);
t.Start();
}
private void UpdateNumerics(object sender, EventArgs e)
{
++_count;
foreach (object textBlock in numericsListBox.Items)
{
var t = …Run Code Online (Sandbox Code Playgroud) 为了在我的WPF表单中记录用户操作,我添加了一些全局事件处理程序
我想准确记录哪个控件触发事件,是否有UIElement像ASP.Net中的ClientId这样的wpf的唯一标识符?
我有以下情况(简化):
在我的ResourceDictionary中,我为Label定义了几个命名样式:
<Style
x:Key="sNormalLabel"
TargetType="Label" >
<Setter Property="FontFamily" Value="Verdana" />
<Setter Property="FontSize" Value="12" />
<!-- ... -->
</Style>
<Style
x:Key="sHeaderLabel"
TargetType="Label"
BasedOn="{StaticResource sNormalLabel}" >
<Setter Property="FontSize" Value="16" />
</Style>
Run Code Online (Sandbox Code Playgroud)
然后我通过使用BasedOn引用它,使其中一个默认为没有键的样式:
<!-- Apply Normal label automatically -->
<Style
TargetType="Label"
BasedOn="{StaticResource sNormalLabel}" />
Run Code Online (Sandbox Code Playgroud)
我认为自动应用标准样式标签非常方便,我知道如果将来我们的设计团队决定使用Wingdings或其他东西,那么整个应用程序很容易改变(事实上所有应用程序都是如此)共享相同的ResourceDictionary).
创建UserControl我想在顶部添加一个带有sHeaderLabel样式的Label,因此我申请
Style={StaticResource sHeaderLabel}
Run Code Online (Sandbox Code Playgroud)
在USerControl XAML中,设计师看起来都很好.
但是,当我将UserControl添加到MainWindow时,标题标签返回到sNormalLabel样式(自动应用的样式).
我假设它与应用样式的顺序有关,UserControl在创建后由MainWindow设置样式,sHeaderLabel样式被自动样式覆盖.
这给我留下了几个悬而未决的问题:
如果有人对类似的东西有任何实际经验,我会很感激.
编辑:有趣的是,我刚刚意识到我只看到Font*属性的这些问题.在Normal/Header样式中以不同方式设置Foreground/Background画笔会使用CORRECT颜色将UserControl中的Label(使用sHeaderLabel设置样式)保留.但是,在sHeaderLabel中将FontWeight设置为粗体无效.
我也以相同的方式设置TextBox样式,使用HeaderTExtBox样式,我也没有看到问题,只有标签.
干杯!
./Fredrik
如何在标签中添加换行符.我有一个标签,其中我添加了一个文本,将导致此显示
A B C i在WinForms c#中完成了这个,Environment.NewLine但这在WPF应用程序中不起作用
有人可以告诉我如何在标签中添加换行符?
我是从代码背后做的,而不是 XAML
我有一个带有DataTemplate的ListBox.我正在尝试将列表框中所选项目的文本/前景颜色更改为白色.我已经认真尝试了30种不同的方法,我在google上找到了它.我无法让它发挥作用.如何更改前景色?
另外,我想指出我不想依赖使用SystemColors的方法,因为我读到它在.net 4.5中不起作用所以我不希望它在我们将应用程序移动到有一天4.5.这是我的列表框xaml:
<ListBox Grid.Row="1" x:Name="Alerts" ItemsSource="{Binding Alerts}" HorizontalContentAlignment="Stretch" ScrollViewer.HorizontalScrollBarVisibility="Disabled" AlternationCount="2">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid Grid.Column="0">
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Orientation="Horizontal">
<Label Content="{Binding Type}" HorizontalAlignment="Left" Padding="1,1,1,0" />
<Label Content=" - " Padding="1,1,1,0"></Label>
<Label Content="{Binding Date}" HorizontalAlignment="Left" Padding="1,1,1,0" />
</StackPanel>
<Label Grid.Row="1" Content="{Binding Message}" HorizontalAlignment="Left" Margin="0" Padding="1,0,1,1" />
</Grid>
<Button Grid.Column="1"
CommandParameter="{Binding .}"
Command="{Binding Path=DataContext.Commands.RemoveAlertCommand, RelativeSource={RelativeSource AncestorType={x:Type ListBox}}}"
Content="X" HorizontalAlignment="Right" Width="Auto" Height="Auto" Foreground="#FFD40000" FontWeight="Bold" VerticalAlignment="Center" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemContainerStyle> …Run Code Online (Sandbox Code Playgroud)