question

njsokalski avatar image
0 Votes"
njsokalski asked RoyLi-MSFT commented

Setting a UserControl Array Property in XAML

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.

windows-uwp
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

The question is confusing. What do you mean by setting the property in XAML? Using binding or directly set the value? It might be better if you could share why you want to set it in the XAML instead of doing it in the code. What's the excepted behavior you want to get if you could set the property in XAML? Could you please tell me the real scenario about this?

0 Votes 0 ·

I mean directly setting the value. What I want to be able to do is something similar to the following:


<MyControl> <MyControl.Sides> <TravelType>Road</TravelType> <TravelType>Track</TravelType> <TravelType>None</TravelType> <TravelType>Road</TravelType> </MyControl.Sides> </MyControl>


The reason I want to be able to do this is because the items in the array determine the visual appearance of the control, and are static. Good practice is often to UI separate from functionality, and being able to have this in the same place as the other parts of my UI would be good.


0 Votes 0 ·

Does anonymous user-3316 's answer work for you?

0 Votes 0 ·

Is there any update for this issue?

0 Votes 0 ·
PeterFleischer-3316 avatar image
0 Votes"
PeterFleischer-3316 answered njsokalski commented

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

First of all, that XAML uses a ListBox, but more importantly, it binds directly to the property, while my XAML binds to other properties that are generated from this property. But even when I tried using a List vs Array, it didn't let me enter it as XAML. For now, I decided to just make a MarkupExtension since I will always have exactly 4 values in the array, but for future situations in other apps this may not be appropriate.


0 Votes 0 ·

Hi, my demo uses the ListBox only for demo purposes to show the binded values. You ask: "I want to set this property in XAML, but I cannot figure out how." and "What can I do to allow me to set this property in XAML? ". My demo shows how to set property ("Sides") in XAML.

0 Votes 0 ·
PeterFleischer-3316 avatar image
0 Votes"
PeterFleischer-3316 answered

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



unbenannt.png (5.0 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.