Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
typos
Source Link
David Turvey
  • 3k
  • 6
  • 27
  • 29

Ok I've had a look at your example project and think i have a solution.

change the int[] in the viewmodel to a List<int>. I'm not sure why this works. I hope there is no technical reason that list<int> is not suitable for you.

Here is what I have changed in the solution

in the viewmodel

public List<int> CustomData { get { return new List<int>(){0,1,2,3}; } set { } } 

In the arraycontrol codebehind

public static readonly DependencyProperty DataProperty = DependencyProperty.Register("Data", typeof(List<int>), typeof(ArrayControl), new FrameworkPropertyMetadata(new List<int>())); public List<int> Data { get { return (List<int>)GetValue(DataProperty); } set { SetValue(DataProperty, value); } } 

In arraycontrol.xaml. Just added listbox to show data binding working

<UserControl x:Class="UserControlWithArray.Controls.ArrayControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Height="300" Width="300"> <Grid> <TextBlock x:Name="MessageTextBlock" Text="ArrayControl"/> <ListBox ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}, Path=Data}"/> </Grid> </UserControl> 

Ok I've had a look at your example project and think i have a solution.

change the int[] in the viewmodel to a List<int>. I'm not sure why this works. I hope there is no technical reason that list<int> is not suitable for you.

Here is what I have changed in the solution

in the viewmodel

public List<int> CustomData { get { return new List<int>(){0,1,2,3}; } set { } } 

In the arraycontrol codebehind

public static readonly DependencyProperty DataProperty = DependencyProperty.Register("Data", typeof(List<int>), typeof(ArrayControl), new FrameworkPropertyMetadata(new List<int>())); public List<int> Data { get { return (List<int>)GetValue(DataProperty); } set { SetValue(DataProperty, value); } } 

In arraycontrol.xaml. Just added listbox to show data binding working

<UserControl x:Class="UserControlWithArray.Controls.ArrayControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Height="300" Width="300"> <Grid> <TextBlock x:Name="MessageTextBlock" Text="ArrayControl"/> <ListBox ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}, Path=Data}"/> </Grid> </UserControl> 
Source Link
David Turvey
  • 3k
  • 6
  • 27
  • 29

You could try initialising the array when you initialise FrameworkPropertyMetaData on the dependency property.

 new FrameworkPropertyMetadata(new int [256], FrameworkPropertyMetadataOptions.AffectsRender, new PropertyChangedCallback(ViewProperty_Changed)) 

I think that the program might be hitting a null reference exception before it manages to bind the dependency property to the viewmodel property.