Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF 4.0定制面板赢得';在VS 2010 Designer中不显示动态添加的控件_Wpf_Visual Studio 2010_Wpf Controls_Designer - Fatal编程技术网

WPF 4.0定制面板赢得';在VS 2010 Designer中不显示动态添加的控件

WPF 4.0定制面板赢得';在VS 2010 Designer中不显示动态添加的控件,wpf,visual-studio-2010,wpf-controls,designer,Wpf,Visual Studio 2010,Wpf Controls,Designer,我有一个自定义面板控件,我正在尝试在其中动态添加控件。当我运行应用程序时,静态和动态添加的控件会完美地显示出来,但动态控件不会显示在VisualStudio设计器中。仅显示以声明方式放置在XAML中的控件。我目前正在CreateUIElementCollection覆盖中添加动态控件,但我在构造函数中也尝试过,但没有成功 Public Class CustomPanel1 Inherits Panel Public Sub New() End Sub Protected Overrides

我有一个自定义面板控件,我正在尝试在其中动态添加控件。当我运行应用程序时,静态和动态添加的控件会完美地显示出来,但动态控件不会显示在VisualStudio设计器中。仅显示以声明方式放置在XAML中的控件。我目前正在CreateUIElementCollection覆盖中添加动态控件,但我在构造函数中也尝试过,但没有成功

Public Class CustomPanel1
Inherits Panel

Public Sub New()

End Sub

Protected Overrides Function MeasureOverride(ByVal availableSize As System.Windows.Size) As System.Windows.Size
    Dim returnValue As New Size(0, 0)

    For Each child As UIElement In Children
        child.Measure(availableSize)
        returnValue.Width = Math.Max(returnValue.Width, child.DesiredSize.Width)
        returnValue.Height = Math.Max(returnValue.Height, child.DesiredSize.Height)
    Next

    returnValue.Width = If(Double.IsPositiveInfinity(availableSize.Width), returnValue.Width, availableSize.Width)
    returnValue.Height = If(Double.IsPositiveInfinity(availableSize.Height), returnValue.Height, availableSize.Height)

    Return returnValue
End Function

Protected Overrides Function ArrangeOverride(ByVal finalSize As System.Windows.Size) As System.Windows.Size
    Dim currentHeight As Integer
    For Each child As UIElement In Children
        child.Arrange(New Rect(0, currentHeight, child.DesiredSize.Width, child.DesiredSize.Height))
        currentHeight += child.DesiredSize.Height
    Next

    Return finalSize
End Function

Protected Overrides Function CreateUIElementCollection(ByVal logicalParent As System.Windows.FrameworkElement) As System.Windows.Controls.UIElementCollection
    Dim returnValue As UIElementCollection = MyBase.CreateUIElementCollection(logicalParent)

    returnValue.Add(New TextBlock With {.Text = "Hello, World!"})

    Return returnValue
End Function

Protected Overrides Sub OnPropertyChanged(ByVal e As System.Windows.DependencyPropertyChangedEventArgs)
    MyBase.OnPropertyChanged(e)
End Sub
End Class
以及我对这个自定义面板的使用

<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:CustomPanel"
Title="MainWindow" Height="364" Width="434">

<local:CustomPanel1>
    <CheckBox />
    <RadioButton />
</local:CustomPanel1>

</Window>

我刚刚在Visual Studio 2008标准版中试用了您的代码,面板显示在设计器中(请参见下面的屏幕截图)

截图显示

您是否尝试过重新构建项目,关闭XAML窗口并再次打开它,以便设计者可以重新加载

编辑:为了澄清下面的评论,按钮是在XAML中添加的,但是如果我删除按钮,这就是我在VisualStudioDesigner中得到的输出

这是我用来获取此输出的XAML:

<Window x:Class="Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1"
        Title="Window1" Height="300" Width="300">
    <local:CustomPanel1>

    </local:CustomPanel1>
</Window>

多亏了本杰明给我的一些提示,我才明白这一点。看起来您需要在UIElement.Loaded事件处理程序中加载动态添加的控件。在发生此事件之前,设计器显然会覆盖子集合中的值。以下是修复此问题的代码:

Public Class CustomPanel1
Inherits Panel

Private _text As TextBlock

Public Sub New()

End Sub

Protected Overrides Function MeasureOverride(ByVal availableSize As System.Windows.Size) As System.Windows.Size
    Dim returnValue As New Size(availableSize.Width, availableSize.Height)
    If _text IsNot Nothing Then


        For Each child As UIElement In Children
            child.Measure(availableSize)
            returnValue.Width = Math.Max(returnValue.Width, child.DesiredSize.Width)
            returnValue.Height = Math.Max(returnValue.Height, child.DesiredSize.Height)
        Next

        returnValue.Width = If(Double.IsPositiveInfinity(availableSize.Width), returnValue.Width, availableSize.Width)
        returnValue.Height = If(Double.IsPositiveInfinity(availableSize.Height), returnValue.Height, availableSize.Height)
    End If
    Return returnValue
End Function

Protected Overrides Function ArrangeOverride(ByVal finalSize As System.Windows.Size) As System.Windows.Size
    If _text IsNot Nothing Then
        Dim currentHeight As Integer
        For Each child As UIElement In Children
            child.Arrange(New Rect(0, currentHeight, child.DesiredSize.Width, child.DesiredSize.Height))
            currentHeight += child.DesiredSize.Height
        Next
    End If

    Return finalSize
End Function

Private Sub CustomPanel1_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
    _text = New TextBlock With {.Text = "Hello, World!"}
    Children.Add(_text)
End Sub
End Class

您是在XAML中添加按钮还是在面板中动态添加按钮?如果我通过XAML添加控件,我可以准确地看到您看到的内容,但是我无法看到动态添加的内容。您的描述与我看到的完全相同。我期望的结果是显示所有控件(声明性控件和动态控件)。这有什么原因不起作用吗?我已经更新了代码,这样面板就像一个垂直方向的堆栈面板,应该可以让您更好地了解我看到的问题。你现在不应该看到控件堆叠在一起。我看到的是“Hello world”文本以及设计器中的复选框和单选按钮。我想我误解了你的问题。好吧,如果你看到了所有的3个,那么它对你有用。如果我有声明性控件,我只能看到它们。如果删除所有声明性控件,则会看到动态控件。另外,您使用的是哪个版本的VS。你的帖子上说是200,但我想这意味着2008年?也许WPF4有点不同。