在备用程序集/库中为UserControl实现WPF AllowTransparency

时间:2014-03-12 06:35:44

标签: c# wpf dll mvvm assemblies

我有一个用户控件,我已将其移动到一个名为“ MVVMHelpersLibrary ”的中央程序集中,以帮助干掉我的代码,因为我想在几个不同的应用程序中使用它。

这个用户控件只是一个内容边框,我用它来覆盖主应用程序中的工具提示,我已经把它作为圆边的边框。

在主WPF应用程序中,父窗口设置为:

   WindowStyle="None" 

   AllowsTransparency="True"

在同一主应用程序组件中定义并使用XAML时,它会显示透明的边角。

但是当定义位于'MVVMHelpersLibrary'库中并且它被用作引用控件时,它会在控件下方显示一个白色背景,即使我将其设置为null。

以上属性对于Window来说似乎是唯一的,所以我尝试在 MVVMHelpersLibrary 中创建一个Window并设置如上所述的属性,但同样的问题。

无论如何,UserControl似乎也没有继承我能找到的透明属性本身,而且我无法将UserControl变成窗口,因为它无法将其作为工具提示嵌入到主窗口中应用

UserControl派生自另一个从UserControl派生的BaseView类,只是为了添加更多属性和Change Notification,但它的行为与直接继承自UserControl的行为相同。


编辑:使用我的代码 - 为了简短而删除明显且不相关的内容

Tooltip UserControl:

   <views:BaseUserControl x:Class="MVVMHelpersLibrary.Views.FunkyToolTipView"
    xmlns:views="clr-namespace:MVVMHelpersLibrary.Views">
<views:BaseUserControl.Resources>
    <LinearGradientBrush x:Key="metallicBorder" EndPoint="0.998,0.5" StartPoint="0.025,0.5">
        <GradientStop Color="#FF595959" Offset="0"/>
        <GradientStop Color="White" Offset="0.7"/>
        <GradientStop Color="#FF303030" Offset="1"/>
    </LinearGradientBrush>
    <Style TargetType="Border">
        <Setter Property="CornerRadius" Value="5"/>
        <Setter Property="Padding" Value="5"/>
        <Setter Property="BorderThickness" Value="2"/>
        <Setter Property="BorderBrush" Value="{StaticResource metallicBorder}"/>
    </Style>
    <Style x:Key="toolTipHeading" TargetType="TextBlock">
        <Setter Property="FontSize" Value="14"/>
        <Setter Property="FontWeight" Value="Bold"/>
    </Style>
    <Style x:Key="toolTipContent" TargetType="TextBlock" BasedOn="{StaticResource toolTipHeading}">
        <Setter Property="FontSize" Value="13"/>
        <Setter Property="FontWeight" Value="SemiBold"/>
    </Style>
</views:BaseUserControl.Resources>
<Border>
    <StackPanel Width="Auto">
        <TextBlock x:Name="HeadingValue" Text="{Binding Heading}" Style="{StaticResource toolTipHeading}" Width="Auto"/>
        <TextBlock x:Name="BodyValue" Text="{Binding Body}" Style="{StaticResource toolTipContent}" Width="Auto" TextWrapping="Wrap" MaxWidth="270"/>
    </StackPanel>
</Border>

该控件使用2x Dependency属性,因此我可以轻松地将Heading和Body元素注入控件并对其进行适当格式化,例如:

    <TextBox Text="Time" >
          <TextBox.ToolTip>
                  <MVVMViews:FunkyToolTipView Heading="Batch Time" Body="The amount of time to run a batch scan for" />
           </TextBox.ToolTip>
     </TextBox>

你能否提出一个解决方案,我可以保留这些依赖属性,或者至少能够轻松定义工具提示的Body和Heading属性?

2 个答案:

答案 0 :(得分:0)

您不需要声明UserControl来定义自定义ToolTip。您只需在Style中执行此操作即可。尝试这样的事情:

<Style TargetType="{x:Type ToolTip}">
    <Setter Property="ToolTip.Template">
        <Setter.Value>
            <ControlTemplate>
                <Border CornerRadius="8" BorderThickness="1" BorderBrush="Black" 
                    Background="White" Padding="5">
                    <ContentPresenter Content="{TemplateBinding ToolTip.Content}" />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

...

<Grid Background="Red" ToolTip="I'm a ToolTip with a Border" />

enter image description here

请记住,在使用WPF时,有许多事情可以在之前尝试,您需要声明自定义UserControlCustomControl。请阅读MSDN上的Control Authoring Overview页面,了解可能的更多信息。

答案 1 :(得分:0)

原来有可能,我只是忘了导出另一种摆脱丑陋默认背景的一般风格:

<Style TargetType="ToolTip">
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="BorderBrush" Value="Transparent"/>
    <Setter Property="Foreground" Value="White" />
</Style>