Setting a UserControl Array Property in XAML

Nathan Sokalski 4,116 Reputation points
2020-05-07T00:53:18.783+00:00

I have a UserControl with a custom DependencyProperty whose type is an array of an Enumeration. I want to set this property in XAML, but I cannot figure out how. What can I do to set this property in XAML? Although I would prefer an Array, I can use a collection type if absolutely necessary. Here is my definition of the DependencyProperty and it's wrapper:

Public Shared ReadOnly SidesProperty As DependencyProperty = DependencyProperty.Register("Sides", GetType(TravelType()), GetType(DraggableSquare), New PropertyMetadata({TravelType.None, TravelType.None, TravelType.None, TravelType.None}, AddressOf SquareChanged))
Public Property Sides() As TravelType()
    Get
        Return CType(GetValue(SidesProperty), TravelType())
    End Get
    Set(ByVal value As TravelType())
        SetValue(SidesProperty, value)
    End Set
End Property

What can I do to allow me to set this property in XAML? Thanks.

Universal Windows Platform (UWP)
{count} votes

2 answers

Sort by: Most helpful
  1. Peter Fleischer (former MVP) 19,231 Reputation points
    2020-05-07T20:43:15.167+00:00

    Hi, use instead of array a List Of like this:

      Public Shared ReadOnly SidesProperty As DependencyProperty =
            DependencyProperty.RegisterAttached("Sides", GetType(List(Of TravelType)),
              GetType(DraggableSquare), New PropertyMetadata(New List(Of TravelType), AddressOf OnSidesChanged))
    

  2. Peter Fleischer (former MVP) 19,231 Reputation points
    2020-05-08T07:55:42.02+00:00

    Hi, try this little demo:

    MainPage:

    <Page  
        x:Class="UWP10App1VB.Page20"  
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
        xmlns:local="using:UWP10App1VB"  
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
        mc:Ignorable="d"  
        Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">  
      <Grid>  
        <local:Page20UC1>  
          <local:Page20UC1.Sides>  
            <local:Window019Type>Road</local:Window019Type>  
            <local:Window019Type>Track</local:Window019Type>  
            <local:Window019Type>None</local:Window019Type>  
          </local:Page20UC1.Sides>  
        </local:Page20UC1>  
      </Grid>  
    </Page>  
    

    XAML UserControl:

    <UserControl  
        x:Class="UWP10App1VB.Page20UC1"  
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
        xmlns:local="using:UWP10App1VB"  
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
        mc:Ignorable="d"  
        d:DesignHeight="300"  
        d:DesignWidth="400">  
      <Grid x:Name="grd">  
        <ListBox ItemsSource="{Binding Sides}"/>  
      </Grid>  
    </UserControl>  
    

    CodeBehind UserControl and Enum:

    Public NotInheritable Class Page20UC1  
      Inherits UserControl  
      
      Public Sub New()  
      
        ' This call is required by the designer.  
        InitializeComponent()  
      
        ' Add any initialization after the InitializeComponent() call.  
        grd.DataContext = Me  
      End Sub  
      
      Public Shared ReadOnly SidesProperty As DependencyProperty =  
            DependencyProperty.RegisterAttached("Sides", GetType(List(Of Window019Type)),  
              GetType(Page20UC1), New PropertyMetadata(New List(Of Window019Type), AddressOf OnSidesChanged))  
      
      Public Shared Function GetSides(obj As DependencyObject) As List(Of Window019Type)  
        Return CType(obj.GetValue(SidesProperty), List(Of Window019Type))  
      End Function  
      
      Public Shared Sub SetSides(obj As DependencyObject, value As List(Of Window019Type))  
        obj.SetValue(SidesProperty, value)  
      End Sub  
      
      Private Shared Sub OnSidesChanged(d As DependencyObject, e As DependencyPropertyChangedEventArgs)  
        Debug.Print("OnSidesChanged")  
      End Sub  
      
    End Class  
      
    Public Enum Window019Type  
      Road  
      Track  
      None  
    End Enum  
    

    Result:

    8061-unbenannt.png

    0 comments No comments