Referencing XAML user control names and properties in VB code behind

Sorry for the basic questions, but I've searched the web for a few days and can't find an answer to these questions.

I have created a custom control and I will be hosting a large number of instances of this custom control in my xaml page. When working with this custom control in VB code, how do I do this?

  • How do I refer to the Custom Control name (in my VB code) that was clicked using the MouseLeftButtonDown event? For example, if I have 10 instances of my custom control in xaml, each with a different name x (eg 1-10), when a specific instance is clicked, how can I see which one was clicked? I've tried several things including e.OriginalSource.Name (which returns the component inside the control that was clicked, not the name of the instance of the control).

  • My Custom Control consists of many parts and parts (Rectangles, Lines, Text, etc.). Each of these elements are part of my layer. In VB code, once I can refer to a specific control, how can I hide or change specific parts of that control (like hiding a line and changing text). Also, I need to change more than just the control that was clicked, so I need to be able to access the properties of all controls, not just what was clicked. For example, if I click Test1, I also need to change Test2, Test3, and Test5 accordingly.

Here is some test code I put together as part of a Silverlight project using MS Blend 2. My control is much larger and I need 200 - 250 instances / copies of this custom control, so I really need to know which instance / copy control was clicked ...

My UserControl:

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

      

My xaml app using a custom control:

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

      

My custom VB control code:

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 

      

Thanks in advance and sorry to all the C # experts I need in VB.

0


a source to share


1 answer


It sounds like you are talking about User Control and not User Control. There is a slight difference when working with them. Read more about these differences here:

Custom Controls Vs. User controls

In this case, you want to look at the 'sender' object in an event handler. You want to serve the sender as your User Control (this will be safe as you only use this event handler for controls of your type).



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

      

I can't compile the code to double check myself at the moment ... but it should give you this idea.

+1


a source







All Articles