在VB Code Behind中引用XAML自定义控件名称和属性

时间:2009-05-13 23:27:49

标签: vb.net xaml

对不起基本问题,但我已经在网上搜索了好几天,但找不到这些问题的答案。

我已经创建了一个自定义控件,我将在我的xaml页面上放置大量的自定义控件实例。在使用VB Code Behind中的自定义控件时,如何执行以下操作?

  1. 如何引用使用MouseLeftButtonDown事件单击的自定义控件的名称(在我的VB代码中)?例如,如果我在xaml中有10个自定义控件实例,每个实例都有不同的x:name(比如1-10),当单击某个特定实例时,如何查看哪个实例被点击了?我尝试过很多东西,包括e.OriginalSource.Name(它返回被点击的控件中的组件,而不是控件实例的名称)。

  2. 我的自定义控件由许多零件组成(矩形,线条,文本等)。这些项目中的每一项都是我的图层的一部分。在VB代码中,一旦我可以引用特定的控件,我如何隐藏或更改该控件的某些部分(例如隐藏线条和更改文本)。此外,我需要修改的不仅仅是单击的控件,因此我需要能够访问所有控件的属性,而不仅仅是单击的内容。例如,如果我单击控件实例Test1,我还需要以某种方式修改Test2,Test3和Test5。

  3. 以下是一些测试代码,我将使用MS Blend 2作为Silverlight项目的一部分。我的控件要大得多,我需要200到250个实例/副本的自定义控件,所以我真的需要知道哪个控件单击了实例/副本。

    我的用户控件:

    <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"
        mc:Ignorable="d"
        x:Class="MyControl1"
        x:Name="UserControl"
        d:DesignWidth="60" d:DesignHeight="59">
    
        <Grid x:Name="LayoutRoot" MouseLeftButtonDown="OnMouseClick">
            <Rectangle x:Name="Rectangle1" Fill="#FFFFFFFF" Stroke="#FF000000"/>
            <TextBox Background="{x:Null}" x:Name="TextBox1" Text="Test" TextWrapping="Wrap"/>
            <Ellipse x:Name="Circle1" Fill="{x:Null}" Stroke="#FF000000"/>
            <Path Margin="1,29,0,29" x:Name="Line1" Fill="{x:Null}" Stretch="Fill" Stroke="#FF000000" Data="M74,80 L132,80"/>
            <Path Margin="0,0,1,14" x:Name="Line2" VerticalAlignment="Bottom" Height="1" Fill="#FFFFFFFF" Stretch="Fill" Stroke="#FF000000" Data="M73,95 L131,95"/>
            <Path Margin="0,0,0,4" x:Name="Line3" VerticalAlignment="Bottom" Height="1" Fill="#FFFFFFFF" Stretch="Fill" Stroke="#FF000000" Data="M73,105 L132,105"/>
        </Grid>
    </UserControl>
    

    我的xaml App使用自定义控件:

    <Grid x:Name="LayoutRoot">
        <Tester:MyControl1 HorizontalAlignment="Left" Margin="56,54,0,0" VerticalAlignment="Top" Width="60" Height="60" x:Name="Test1"/>
        <Tester:MyControl1 HorizontalAlignment="Left" Margin="116,54,0,0" VerticalAlignment="Top" Width="60" Height="60" x:Name="Test2"/>
        <Tester:MyControl1 HorizontalAlignment="Left" Margin="176,54,0,0" VerticalAlignment="Top" Width="60" Height="60" x:Name="Test3"/>
        <Tester:MyControl1 HorizontalAlignment="Left" Margin="236,54,0,0" VerticalAlignment="Top" Width="60" Height="60" x:Name="Test4"/>
        <Tester:MyControl1 HorizontalAlignment="Left" Margin="296,54,0,0" VerticalAlignment="Top" Width="60" Height="60" x:Name="Test5"/>
    </Grid>
    

    我的自定义控件VB代码:

    Partial Public Class MyControl1
        Public Sub New()
            MyBase.New()
    
            Me.InitializeComponent()
    
            ' Insert code required on object creation below this point.
        End Sub
    
        Private Sub OnMouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
    
            Dim int_Temp As Integer
            Dim str_InstanceName As String
    
            str_InstanceName = "1.What code here tells me the name of the instance which was checked?  Test1, Test2, etc. for example."
    
            int_Temp = MessageBox.Show(str_InstanceName, "Testing", MessageBoxButton.OK)
    
            '2.What code here lets me manipulate parts of my control instances (and not just the instance which was clicked)?
    
                    'I want to hide Test1.Line1 and Test2.Line3 and Test3.Circle1 and change the background of Test5.Rectangle1 for example.
    
        End Sub
    End Class 
    

    提前致谢,对于我在VB中需要的所有C#专家感到抱歉。

1 个答案:

答案 0 :(得分:1)

看起来你在谈论用户控件,而不是自定义控件。与两者合作时会有一些区别。您可以在此处详细了解这些差异:

Custom Controls Vs. User Controls

在这种情况下,您希望查看事件处理程序中的“sender”对象。您希望将发件人转换为您的用户控件(这将是安全的,因为您只在您的类型的控件上使用该事件处理程序。)

Private Sub OnMouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)

    Dim senderAsControl As MyControl1 = sender As MyControl1

    ' Get the instance name from the sender
    Dim instanceName As String = senderAsControl.Name

    ' You can also access your children from the sender once cast
    senderAsControl.Rectangel1.IsVisible = False ' Hide the rectangle

End Sub

我目前无法编译代码来仔细检查自己......但它应该会给你一个想法。

相关问题