wpf - XAML/WPF 中的 UserControls 有什么问题?

标签 wpf vb.net xaml user-controls

我在让 UserControls 在 XAML 中工作时遇到了重大问题 - 我花了几个小时试图找出所有问题,但一无所获,也找不到我出错的地方。
我遇到的主要问题是当我创建一个用户控件时,例如一个显示对象不同颜色的简单控件 - 我已成功为此创建了一个属性,并且可以在设计时将颜色分配给该用户控件并且它可以工作 - 显示绿色,红色等
然而,当我给这个 UserControl 一个名称,以便我可以在运行时分配这个属性时,如果我删除用户控件工作的名称,我会收到错误“无法创建类型'MyUserControl'的实例” - 我可以添加任意数量的设计时,它们都可以工作,但是一旦我分配了名称或 x:Name,它就会中断,我无法弄清楚为什么。
例如,可以创建一个标签,并且它有一个名称,我可以在代码中引用它 - 为什么不是我自己的控件 - 无论多么简单。

我的主要问题是:

  1. 为什么给我的 UserControl Name 或 x:name 阻止它工作吗?
  2. 如何使用多个 同一类型的用户控件 window ?
  3. 如何访问 Canvas 、标签 来自内部的 UserControl 等或 在用户控件之外?
  4. 如何实例化 UserControl 运行时还是在代码或 XAML 的设计时?

我不明白为什么这些会如此困难 - 我无法解决这个问题,所以如果有人可以帮忙,请帮忙,谢谢!


这是我的 UserControl 的 XAML 和代码

<UserControl x:Class="Device.Draco"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="20" Height="36" x:Name="Icon">
    <Canvas Width="20" Height="36" HorizontalAlignment="Left" VerticalAlignment="Top">
        <Rectangle Height="36" Width="20" Fill="{Binding ElementName=Icon, Path=ZuneColour}" Canvas.Left="0" Canvas.Top="0" RadiusX="1" RadiusY="1">
            <Rectangle.BitmapEffect><OuterGlowBitmapEffect GlowColor="Black" GlowSize="2" /></Rectangle.BitmapEffect>
        </Rectangle>
        <Rectangle Canvas.Left="1" Canvas.Top="1" Height="24" Stroke="#191616" Width="18">
            <Rectangle.Fill>
                <LinearGradientBrush>
                    <GradientStop Offset="1" Color="#231F20"/>
                    <GradientStop Offset="0" Color="#524F4F"/>
                    <LinearGradientBrush.Transform>
                        <RotateTransform Angle="68" CenterX="0.5" CenterY="0.5"/>
                    </LinearGradientBrush.Transform>
                </LinearGradientBrush>
            </Rectangle.Fill>
        </Rectangle>
        <Rectangle Canvas.Left="5.5" Canvas.Top="25" Height="9" Width="9" RadiusX="3" RadiusY="3">
            <Rectangle.Fill>
                <LinearGradientBrush>
                    <GradientStop Offset="0" Color="#66000000"/>
                    <GradientStop Offset="1" Color="#22000000"/>
                </LinearGradientBrush>
            </Rectangle.Fill>
            <Rectangle.Stroke>
                <LinearGradientBrush>
                    <GradientStop Offset="0" Color="#66FFFFFF"/>
                    <GradientStop Offset="1" Color="#22FFFFFF"/>
                </LinearGradientBrush>
            </Rectangle.Stroke>
        </Rectangle>
    </Canvas>

这是 UserControl 的代码 - 已添加所有样式并填充为简单的 XAML,以消除此原因 - 隐藏代码如下:

Namespace Device
    Partial Public Class Draco
        Inherits System.Windows.Controls.UserControl
        Public Shared ZuneColorProperty As DependencyProperty = _
        DependencyProperty.Register("ZuneColour", GetType(Brush), GetType(Device.Draco))
        Public Property ZuneColour() As Brush
            Get
                Return GetValue(ZuneColorProperty)
            End Get
            Set(ByVal Value As Brush)
                SetValue(ZuneColorProperty, Value)
            End Set
        End Property
    End Class
End Namespace

这是我目前如何使用它的示例

<Window x:Class="Demo"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:ui="clr-namespace:ZuneCards.Device"
    Title="Demo" Height="300" Width="300" Name="Window1">
    <Grid>
        <ui:Draco ZuneColour="Pink" HorizontalAlignment="Right" Margin="0,113,81,113" Width="20"></ui:Draco>
    </Grid>
</Window>

最佳答案

如果不查看 UserControl 的源代码,我不确定是否可以解决您的具体问题。

但是我可以回答第 3 部分。如果您为控件模板的一部分指定名称,则可以从控件代码中检索该部分。如果需要,您可以从那里公开返回它。

<ControlTemplate TargetType="{x:Type l:MyUserControl}">
    <Button x:Name="PART_button">My Button</Button>
</ControlTemplate>

[TemplatePart(Name = "PART_button", Type = typeof(Button))]
public class MyUserControl:UserControl
{

    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();

        Button button = (Button)base.GetTemplateChild("PART_button");

        // do something with the button control
    }

}

该属性表示该控件在其模板中需要一个 Button 类型的“PART_button”控件。然后在 OnApplyTemplate 方法中,您可以调用 GetTemplateChild 来检索您需要的部分。

关于wpf - XAML/WPF 中的 UserControls 有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/506702/

相关文章:

c# - 将正则表达式从 .NET 转换为 javascript 时出错

c# - WPF XAML - 创建一个带有边框、路径和文本相互叠加的按钮,所有样式都由按钮控制

.net - 将列表框项目的点击区域拉伸(stretch)到列表框的整个宽度? ListBox 样式通过主题隐式设置

xaml - 使用 VisualStateManager 对按钮的 ScaleTransform 进行动画处理

WPF:指针下的颜色

C# 到 VB - 如何转换此匿名方法/lambda 表达式?

c# - 将原始处理程序的事件参数传递给 wpf 中的路由事件

asp.net - 将单选按钮列表绑定(bind)到 gridview 中的列

c# - 来自 xaml 的 WPF 控件引用在代码端不可见

wpf - WPF样式 setter 中的多个运行元素