相关疑难解决方法(0)

Label和TextBlock之间的区别

根据Windows应用程序开发与Microsoft .NET 4 70-511培训工具包

Label控件和TextBlock控件有什么区别,因为它们都是内容控件而只是显示文本?

wpf label textblock

123
推荐指数
4
解决办法
6万
查看次数

是否期望这种慢速WPF 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)

c# wpf performance textblock

18
推荐指数
1
解决办法
9226
查看次数

是否有任何wpf UIElement的唯一标识符?

为了在我的WPF表单中记录用户操作,我添加了一些全局事件处理程序

我想准确记录哪个控件触发事件,是否有UIElement像ASP.Net中的ClientId这样的wpf的唯一标识符?

c# wpf wpf-controls uielement

8
推荐指数
2
解决办法
2万
查看次数

WPF默认样式和UserControls - 最佳实践?

我有以下情况(简化):

在我的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样式被自动样式覆盖.

这给我留下了几个悬而未决的问题:

  1. 像我一样使用自动应用的样式是不常见并且与最佳实践相反?我认为这很实用,但也许这是错误的方法.
  2. 有没有办法明确排除某些控件应用这些自动样式?我能想到的唯一方法是使用HeaderLabel扩展Label并应用不同的资源键,以便HeaderLabel自动应用sHeaderLabel样式.还有其他方法吗?

如果有人对类似的东西有任何实际经验,我会很感激.

编辑:有趣的是,我刚刚意识到我只看到Font*属性的这些问题.在Normal/Header样式中以不同方式设置Foreground/Background画笔会使用CORRECT颜色将UserControl中的Label(使用sHeaderLabel设置样式)保留.但是,在sHeaderLabel中将FontWeight设置为粗体无效.

我也以相同的方式设置TextBox样式,使用HeaderTExtBox样式,我也没有看到问题,只有标签.

干杯!

./Fredrik

.net wpf styles

5
推荐指数
1
解决办法
1144
查看次数

如何在标签中添加换行符

如何在标签中添加换行符.我有一个标签,其中我添加了一个文本,将导致此显示

A B C i在WinForms c#中完成了这个,Environment.NewLine但这在WPF应用程序中不起作用

有人可以告诉我如何在标签中添加换行符?

我是从代码背后做的,而不是 XAML

wpf c#-4.0

3
推荐指数
2
解决办法
8735
查看次数

如何在列表框中更改所选项目"文本"前景色

我有一个带有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)

c# wpf xaml wpf-controls

1
推荐指数
1
解决办法
3334
查看次数

标签 统计

wpf ×6

c# ×3

textblock ×2

wpf-controls ×2

.net ×1

c#-4.0 ×1

label ×1

performance ×1

styles ×1

uielement ×1

xaml ×1