【问题标题】:Add Usercontrol To A Canvas将用户控件添加到画布
【发布时间】:2012-09-05 12:51:20
【问题描述】:

我见过其中一个例子 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 代码

    <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>

添加很多标签后。

【问题讨论】:

  • lableList List
  • 我以为标签在另一个控件后面丢失了。但事实并非如此......
  • 我有一个建议,请使用 snoopwpf.codeplex.com - WPF 间谍实用程序安装并检查正在运行的应用程序中的 wpf 元素。

标签: c# wpf xaml canvas user-controls


【解决方案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

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

【讨论】:

  • 我添加了我的 XML,你可以看看!
  • ScrollViewer 在其他网格中,即在 Usercontrol 中
【解决方案2】:

我已尝试重现您的问题,但我怀疑可能的问题应该是这样的

  • 没有为标签控件设置前景色。
  • 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);
        }
    }
}

【讨论】:

    【解决方案3】:

    使用你的 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);
        }
    

    我可以添加标签

    【讨论】:

      【解决方案4】:

      问题是我使用了样式,而且我的标签写得太多了,我用文本框替换了它,一切看起来都很好....

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-16
        • 2012-07-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多