简体   繁体   English

WPF:样式化ContentControl

[英]WPF: Styling ContentControl

I would like to create a style that could be applied to any ContentControl which would take the ToolTip and add an image to the ContentControl and apply the tool tip text from the object to the image. 我想创建一种可以应用于任何ContentControl的样式,该样式将使用ToolTip并将图像添加到ContentControl并将工具提示文本从对象应用于图像。 I have about a hundred of these that need to be done (in various projects) so being able to create a style would save a lot of typing. 我有大约一百个需要完成的工作(在各个项目中),因此能够创建样式可以节省很多打字工作。

What I am trying to recreate is this (ToolTip text is on the blue 'i' and not the 'Reload Employee Data': 我想重新创建的是这个(工具提示文本在蓝色的“ i”上,而不是在“重新加载员工数据”上:

在此处输入图片说明

which is accomplished via the following: 这是通过以下步骤完成的:

<StackPanel Orientation="Horizontal">
    <CheckBox Content="Reload Employee Data"
              IsChecked="{Binding AdjustmentSettings.ReloadEmployeeData}" 
              Grid.Row="0"
              Grid.Column="0">
    </CheckBox>
    <Image Source="/DelphiaLibrary;Component/Resources/info.ico" 
           ToolTip="Check if you want to re-upload ..."/>
</StackPanel>

What I am trying to avoid is creating a new stack panel each time I want to add the blue 'i' with the tool tip text on the 'i' and not on the text of the object. 我要避免的是每次我想添加蓝色的“ i”时都创建一个新的堆栈面板,并且工具提示文本位于“ i”而不是对象的文本上。

I was able to create the following that works for a Label: 我能够创建适用于标签的以下内容:

    <!-- Works for just Label -->
    <Style x:Key="LabelToolTipStyle"
           TargetType="{x:Type Label}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Label">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{TemplateBinding Content}" />
                        <Image Source="info.ico" ToolTip="{TemplateBinding ToolTip}"/>
                    </StackPanel>

                </ControlTemplate>                    
            </Setter.Value>
        </Setter>
    </Style>

And I can call this simply by adding the style to the label like so: 我可以简单地通过将样式添加到标签中来调用它,如下所示:

<Label Content="First Text" Style="{StaticResource LabelToolTipStyle}" ToolTip="Label with LabelToolTipStyle" />

I then tried to make this more generalized by creating a style targeting ContentControl but obviously doesn't work because this overrides the entire template (in the case of CheckBox control, the checkbox is missing): 然后,我尝试通过创建一种针对ContentControl的样式来使其更通用,但显然不起作用,因为这会覆盖整个模板(在CheckBox控件的情况下,该复选框丢失了):

    <!-- Works on Label but not CheckBox -->
    <Style x:Key="ContentToolTipStyle"
           TargetType="{x:Type ContentControl}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ContentControl">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{TemplateBinding Content}" />
                        <Image Source="info.ico" ToolTip="{TemplateBinding ToolTip}"/>
                    </StackPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

Is there a way that I could create a style for ContentControls that would allow me to ADD to the template without redefining the entire template? 有没有一种方法可以为ContentControls创建样式,使我可以在不重新定义整个模板的情况下添加到模板? If it cannot be done to ContentControl I wouldn't be opposed to creating a separate style for each control type but would like to avoid redefining the entire template to do so. 如果无法对ContentControl做到这一点,那么我不会反对为每种控件类型创建单独的样式,而是希望避免重新定义整个模板。

You are almost there. 你快到了。 You need to create a custom control template for a ContentControl : 您需要为ContentControl创建一个自定义控件模板:

<Style x:Key="ToolTipWrapper" TargetType="{x:Type ContentControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ContentControl}">
                <StackPanel Orientation="Horizontal">
                    <StackPanel.ToolTip>
                        <ToolTip Visibility="Hidden" />
                    </StackPanel.ToolTip>
                    <ContentPresenter />
                    <Image Source="info.ico" ToolTip="{TemplateBinding ToolTip}" />
                </StackPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Then wrap your elements in a ContentControl and apply the style: 然后将元素包装在ContentControl并应用样式:

<ContentControl Style="{StaticResource ToolTipWrapper}" ToolTip="Hello world">
    <CheckBox Content="I am a check box" />
</ContentControl>

What you can't do is to automatically apply the custom style to all "content" controls: you will always need the extra ContentControl wrapped around each element you want to style in this way. 无法做的是将自定义样式自动应用于所有 “内容”控件:您将始终需要用这种方式围绕每个要设置样式的元素包装额外的ContentControl

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM