wpf - Canvas 背景颜色不显示

标签 wpf silverlight user-controls windows-phone-7

我创建了一个 UserControl 以使用 TextBlock 中的 Canvas 显示弹出窗口。一切似乎都正常,除了 Canvas 的背景颜色。无论我尝试什么,它都被渲染为透明。我还尝试添加一个 Rectangle 并填充它,但这也不起作用。这是代码:

<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:Custom="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
             xmlns:ic="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions"
             xmlns:System="clr-namespace:System;assembly=mscorlib"
             mc:Ignorable="d"
             x:Class="PopupText.CanvasPopup"
             d:DesignWidth="456"
             d:DesignHeight="129">

  <StackPanel x:Name="LayoutRoot"
              Orientation="Horizontal">

        <!--This button toggles the visibility of the popup-->
    <Button x:Name="ActivateButton"
            HorizontalAlignment="Left"
            VerticalAlignment="Top"
            BorderThickness="0"
            Click="OnActivateButtonClicked">

      <Image Source="/pushpin.png"
             Width="36"
             Height="36"
             HorizontalAlignment="Center"
             VerticalAlignment="Center"
             Stretch="Fill"
             Margin="0" />

    </Button>

    <Canvas x:Name="PopupContainer"
            HorizontalAlignment="Left"
            VerticalAlignment="Top"
            Width="{Binding Width, ElementName=PopupText}"
            Height="{Binding Height, ElementName=PopupText}"
            Visibility="Collapsed">
      <Canvas.Background>
        <LinearGradientBrush EndPoint="0.5,1"
                             StartPoint="0.5,0">
          <GradientStop Color="Black"
                        Offset="0" />
          <GradientStop Color="#7FA79797"
                        Offset="1" />
        </LinearGradientBrush>
      </Canvas.Background>
      <Rectangle Canvas.Left="0"
                 Canvas.Top="0"
                 RadiusX="20"
                 RadiusY="20"
                 Width="{Binding Width, ElementName=PopupText}"
                 Height="{Binding Height, ElementName=PopupText}">
        <Rectangle.Fill>
          <LinearGradientBrush EndPoint="0.5,1"
                               StartPoint="0.5,0">
            <GradientStop Color="Black"
                          Offset="0" />
            <GradientStop Color="#7F67749D"
                          Offset="1" />
          </LinearGradientBrush>
        </Rectangle.Fill>
        <Rectangle.Stroke>
          <LinearGradientBrush EndPoint="0.5,1"
                               StartPoint="0.5,0">
            <GradientStop Color="Black"
                          Offset="0" />
            <GradientStop Color="#7FC89B9B"
                          Offset="1" />
          </LinearGradientBrush>
        </Rectangle.Stroke>
      </Rectangle>
      <TextBlock x:Name="PopupText"
                 Text="A really long line of text that will either overflow or wrap"
                 TextWrapping="Wrap"
                 Width="350"
                 Canvas.Left="0"
                 Canvas.Top="0" />
    </Canvas>
  </StackPanel>
</UserControl>

谢谢你的帮助!

最佳答案

看起来您想绑定(bind) Canvas 的大小到 TextBlock 的实际大小不是在设计时指定的值。

在这种情况下,使用这样的绑定(bind):

Width="{Binding ActualWidth, ElementName=PopupText}"
Height="{Binding ActualHeight, ElementName=PopupText}">

关于wpf - Canvas 背景颜色不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4575921/

相关文章:

WPF 将 ICommand 绑定(bind)到事件 (FrameworkElement.Unloaded)

c# - RichTextBox - UI 调整大小导致巨大的 CPU 负载

wpf - 如何防止任务栏出现在 WPF 非全屏窗口中?

silverlight - Silverlight 4 中的可序列化属性

wpf - itemspresenter、contentpresenter、itemscontrol、contentcontrol 之间的区别

c# - 用户控件 - 从数据源的属性名称列表中选取属性值

WPF - 在 UserControl 中托管内容

wpf - 如何支持回车键使焦点在 ListView 的文本栏中移动

c# - WPF 从多个 View 模型将选项卡项添加到选项卡控件

silverlight - 学习 WCF RIA 服务的最佳资源