English 中文(简体)
世界森林论坛的“灯塔”,使用风格
原标题:Underline Label in WPF, using styles
  • 时间:2009-09-27 17:56:19
  •  标签:

我的风格如下:

<Style x:Key="ActionLabelStyle" TargetType="{x:Type Label}">
    <Setter Property="Margin" Value="10,3" />
    <Setter Property="Padding" Value="0" />
    <Setter Property="TextBlock.TextWrapping" Value="Wrap" />
    <Setter Property="FontFamily" Value="Calibri" />
    <Style.Triggers>
        <MultiTrigger>
            <MultiTrigger.Conditions>
                <Condition Property="IsMouseOver" Value="True" />
                <Condition Property="IsEnabled" Value="True" />
            </MultiTrigger.Conditions>
            <Setter Property="Background" Value="Red" />
            <Setter Property="TextBlock.TextDecorations" Value="Underline" />
        </MultiTrigger>
    </Style.Triggers>
</Style>

因此,我基本上想有一个标签,当它能够使用时,它就会得到强调,而 mo的 cur子已经倒了。 这一风格中未发挥作用的部分是:<Setter Property=“TextBlock.TextDecorations” Value=“Online”/>。 现在,我在这里做了什么错误? 感谢一切帮助。

最佳回答

这实际上比现在困难得多。 在妇女论坛中,“Label”不是案文Block。 它源自内容调查,因此可以在内容收集方面接受其他非文本控制。

但是,你可以具体说明如下例子的内容。 在内部,将建造一个会议室,为您提供发言稿。

<Label Content="Test!"/>

这在内部转化为:

    <Label>
        <Label.Content>
            <TextBlock>
                Test!
            </TextBlock>
        </Label.Content>
    </Label>

简单的解决办法是,文本Block的财产作为附属财产。 例如,FontSize就是这样设计的,因此:

    <Label TextBlock.FontSize="24">
        <Label.Content>
            <TextBlock>
                Test!
            </TextBlock>
        </Label.Content>
    </Label>

文本Block。 玉米附属财产可在直观树中任何地方应用,并将高于树木中任何原生后代对该财产的默认价值。 然而,财产不是这样设计的。

这使你们至少有几种选择。

  1. Use color, border, cursor, etc., instead of underlined text because this is 100% easier to implement.
  2. Change the way you are doing this to apply the Style to the TextBlock instead.
  3. Go to the trouble to create your own attached property and the control template to respect it.
  4. Do something like the following to nest the style for TextBlocks that appear as children of your style:

这是我迄今在人民论坛所做的最丑恶的事情,但它却发挥了作用!

    <Style x:Key="ActionLabelStyle" TargetType="{x:Type Label}">
        <Setter Property="Margin" Value="10,3" />
        <Setter Property="Padding" Value="0" />
        <Setter Property="TextBlock.TextWrapping" Value="Wrap" />
        <Setter Property="FontFamily" Value="Calibri" />
        <Style.Triggers>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="IsMouseOver" Value="True" />
                    <Condition Property="IsEnabled" Value="True" />
                </MultiTrigger.Conditions>
                <Setter Property="Background" Value="Red" />
            </MultiTrigger>
        </Style.Triggers>
        <Style.Resources>
            <Style TargetType="TextBlock">
                <Style.Triggers>
                    <MultiDataTrigger>
                        <MultiDataTrigger.Conditions>
                            <Condition Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Label}, Path=IsMouseOver}" Value="True" />
                            <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsEnabled}" Value="True" />
                        </MultiDataTrigger.Conditions>
                        <Setter Property="TextDecorations" Value="Underline"/>
                    </MultiDataTrigger>
                </Style.Triggers>
            </Style>
        </Style.Resources>
    </Style>

这样做的原因在于它超越了这种风格背后的任何文本Block语的缺省风格。 然后,它使用多数据交换机,允许对Label的相对约束性支持,以检查其IsMouseOver财产是否真实。 Yuck。

<><>Edit>:

请注意,只有你明确设立案文Block,才能做到这一点。 我在张贴这封信时是不正确的,因为我已经放弃了我的拉贝尔测试。 Boo。 感谢 Anvaka指出这一点。

    <Label Style="{StaticResource ActionLabelStyle}">
        <TextBlock>Test!</TextBlock>
    </Label>

这样做,但如果你不得不去处理这一麻烦,你就重新努力。 没有人会把东西 post倒,或者正如你所说的那样,我的选择1现在看起来好坏。

问题回答

除杰里的回答外,为了避免在模板中添加文字B锁,每当你能够让风格也这样做时,就应当把“Setter”财产添加到:

<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="{x:Type Label}">
            <TextBlock>
                <ContentPresenter />
            </TextBlock>
        </ControlTemplate>
    </Setter.Value>
</Setter>

之后,你们的Label又回到:

<Label Content="Test!" Style="{StaticResource ActionLabelStyle}" />

感谢杰里!

Andrew.

我认为,问题在于<条码>TextBlock.TextDecorations没有在

您可使用,如果您愿意使用TextBlock而不是

仅补充我的工作。 我目前正在使用C#代码,而且它运作得足够。 我只把摩塞莱维和摩埃塞特事件捉.见肘,并向那里展示了强调。

void Control_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
    WPFHelper.EnumerateChildren<TextBlock>(this, true).ForEach(c => c.TextDecorations = null);
}

void Control_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
    WPFHelper.EnumerateChildren<TextBlock>(this, true).ForEach(c => c.TextDecorations = TextDecorations.Underline);
}

“妇女论坛”班只列举所有受抚养人的子女,而“每个孩子”是一种延伸方法,只是为了每个项目在“玛尔布达”的表述中采取行动。

一个老问题,但既然我刚刚与这个问题作斗争,我就这样做了。 虽然这只是使用Trigger而不是多Trigger

For XAML:

<Label Content="This text is for testing purposes only.">
    <Style TargetType="{x:Type ContentPresenter}">
        <Style.Resources>
            <Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource {x:Type TextBlock}}">
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="TextDecorations" Value="Underline" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Style.Resources>
    </Style>
</Label>

C# Codebehind:

public override void OnApplyTemplate()
{
    if( !hasInitialized )
    {
        var tbUnderStyle = new Style( typeof(TextBlock), (Style)FindResource(typeof(TextBlock)) );
        var tbUnderSetter = new Setter( TextBlock.TextDecorationsProperty, TextDecorations.Underline );
        var tbUnderTrigger = new Trigger() { Property = Label.IsMouseOverProperty, Value = true };
        tbUnderTrigger.Setters.Add( tbUnderSetter );
        var contentPresenter = FindVisualChild<ContentPresenter>( this );
        contentPresenter.Resources.Add( typeof(TextBlock), tbUnderStyle );

        hasInitialized = true;
    }
}

has In In construct





相关问题
热门标签