将Usercontrol添加到画布

时间:2012-09-05 12:51:21

标签: c# wpf xaml canvas user-controls

我见过其中一个例子 Add WPF control at runtime 似乎这个解决方案适用于很多人。我到底做错了什么?我的标签不会在画布上显示。

Label l = new Label();
l.Background = new LinearGradientBrush(Colors.Black, Colors.Black, 0);
canBackArea.Children.Add(l);
l.Visibility = System.Windows.Visibility.Visible;
l.Content = "Hello";
Canvas.SetLeft(l,20); 
Canvas.SetTop(l, 20);
Canvas.SetZIndex(l, lableList.Count);

画布有白色,因此背景。 canBackArea是一个画布

XML CODE

    <ScrollViewer Grid.Row="2" HorizontalScrollBarVisibility="Visible">
        <Canvas Name="canBackArea"
                Width="500"
                Height="300"
                HorizontalAlignment="Left"
                VerticalAlignment="Top"
                Background="White"
                MouseMove="canBackArea_MouseMove">
            <telerik:RadContextMenu.ContextMenu>
                <telerik:RadContextMenu Name="mnuBack"
                                        ItemClick="ContextMenu_ItemClick"
                                        Opened="mnuBack_Opened">
                    <telerik:RadMenuItem Name="mBackground" Header="Set Background Image" />
                    <telerik:RadMenuItem Name="mSize" Header="Set Size" />
                    <telerik:RadMenuItem Name="mLable" Header="Add Text" />
                    <telerik:RadMenuItem Name="mChangeText" Header="Change Text" />
                </telerik:RadContextMenu>
            </telerik:RadContextMenu.ContextMenu>
            <Image Name="imgBackground" />
        </Canvas>
    </ScrollViewer>

添加大量标签后。

4 个答案:

答案 0 :(得分:1)

这是我的MainWindow.xaml

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Canvas x:Name="canBackArea">

</Canvas>

这是我的代码隐藏。

 public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        Label l = new Label();

        canBackArea.Children.Add(l);
        l.Visibility = System.Windows.Visibility.Visible;
        l.Content = "Hello";
        Canvas.SetLeft(l, 20);
        Canvas.SetTop(l, 20);
    }

这完全没问题。

http://i.imgur.com/JooqS.png

这可能取决于您使用它的上下文?

答案 1 :(得分:0)

我试图重现您的问题,我怀疑可能存在的问题

  • 没有前景色设置为标签控件。
  • zIndex应该比任何其他的子控件更多,我认为Canvas.SetZIndex(l,canBackArea.Children.Count);

以下是我尝试和测试的内容。

XAML代码

<Window x:Class="TestApplication.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="50" />
            <RowDefinition Height="269*" />
        </Grid.RowDefinitions>
        <StackPanel Orientation="Horizontal">
            <Label Content="New Label Content" Height="30" />
            <TextBox x:Name="txtLabelContent" Width="200" Height="30"></TextBox>
            <Button Margin="10 0 0 0" Height="30" Width="70" Click="Button_Click">Add Label</Button>
        </StackPanel>
        <Canvas Grid.Row="1" x:Name="canBackArea" Background="White" Grid.RowSpan="2">
        </Canvas>
    </Grid>
</Window>

窗口代码隐藏

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace TestApplication
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Label l = new Label();
            l.Background = new LinearGradientBrush(Colors.Black, Colors.Black, 0);
            l.Foreground = new LinearGradientBrush(Colors.White, Colors.White, 0);
            canBackArea.Children.Add(l);
            l.Visibility = System.Windows.Visibility.Visible;
            l.Content = txtLabelContent.Text;
            Canvas.SetLeft(l, 20);
            Canvas.SetTop(l, 20);
            Canvas.SetZIndex(l, canBackArea.Children.Count);
        }
    }
}

答案 2 :(得分:0)

使用你的xaml和这个

private void mnuBack_ItemClick(object sender, Telerik.Windows.RadRoutedEventArgs e)
    {
        Label l = new Label();

        canBackArea.Children.Add(l);
        l.Visibility = System.Windows.Visibility.Visible;
        l.Content = "Hello";
        Canvas.SetLeft(l, 20);
        Canvas.SetTop(l, 20);
        Canvas.SetZIndex(l, lableList.Count);
        lableList.Add(l);
    }

我可以添加标签

答案 3 :(得分:0)

问题是我正在使用样式,而且是如何过度编写我的标签,我用文本框替换它,每件事情似乎都很好......