Cannot get value of item when set Default value in PropertyMetadata is Array of string

Viewed 48

I have a DependencyProperty in my custom control. I have set the default value of DayOfWeeksProperty as an array of values ​​which are strings and then I set PropertyChangedCallback as a method where TextBlock are set value for each element inside default array.

public string[] DayOfWeeks
        {
            get { return (string[])GetValue(DayOfWeeksProperty); }
            set { SetValue(DayOfWeeksProperty, value); }
        }

        // Using a DependencyProperty as the backing store for DayOfWeeks.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DayOfWeeksProperty =
            DependencyProperty.Register(
                "DayOfWeeks",
                typeof(string[]),
                typeof(ScheduleControl),
                new PropertyMetadata(
                    new string[7] { "Volvo", "BMW", "Ford", "Mazda", "BMW", "Ford", "Mazda" },
                    OnDayOfWeeksChanged)
                );

        private static void OnDayOfWeeksChanged(
            DependencyObject dependencyObject,
            DependencyPropertyChangedEventArgs e)
        {
            ScheduleControl scheduleControl = dependencyObject as ScheduleControl;
            scheduleControl.OnDayOfWeeksChanged(e);
        }

        private void OnDayOfWeeksChanged(DependencyPropertyChangedEventArgs e)
        {
            string[] dayOfWeeks = (string[]) e.NewValue;
            TextBlock_Monday.Text = dayOfWeeks[0];
            TextBlock_Tuesday.Text = dayOfWeeks[1];
            TextBlock_Wednesday.Text = dayOfWeeks[2];
            TextBlock_ThursDay.Text = dayOfWeeks[3];
            TextBlock_Friday.Text = dayOfWeeks[4];
            TextBlock_Saturday.Text = dayOfWeeks[5];
            TextBlock_Sunday.Text = dayOfWeeks[6];
        }

XAML

<UserControl
    x:Class="cs4rsa_core.Controls.ScheduleControl"
    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"
    d:DesignHeight="450"
    d:DesignWidth="800"
    mc:Ignorable="d">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <TextBlock
            x:Name="TextBlock_TimeLineHeading"
            Grid.Row="0"
            Grid.Column="0" />
        <TextBlock
            x:Name="TextBlock_Monday"
            Grid.Row="0"
            Grid.Column="1" />
        <TextBlock
            x:Name="TextBlock_Tuesday"
            Grid.Row="0"
            Grid.Column="2" />
        <TextBlock
            x:Name="TextBlock_Wednesday"
            Grid.Row="0"
            Grid.Column="3" />
        <TextBlock
            x:Name="TextBlock_ThursDay"
            Grid.Row="0"
            Grid.Column="4" />
        <TextBlock
            x:Name="TextBlock_Friday"
            Grid.Row="0"
            Grid.Column="5" />
        <TextBlock
            x:Name="TextBlock_Saturday"
            Grid.Row="0"
            Grid.Column="6" />
        <TextBlock
            x:Name="TextBlock_Sunday"
            Grid.Row="0"
            Grid.Column="7" />
        <ListView Grid.Row="1" Grid.Column="0" ItemsSource="{Binding}" SelectedItem="" />
        <ListView Grid.Row="1" Grid.Column="1" ItemsSource="{Binding}" SelectedItem="" />
        <ListView Grid.Row="1" Grid.Column="2" ItemsSource="{Binding}" SelectedItem="" />
        <ListView Grid.Row="1" Grid.Column="3" ItemsSource="{Binding}" SelectedItem="" />
        <ListView Grid.Row="1" Grid.Column="4" ItemsSource="{Binding}" SelectedItem="" />
        <ListView Grid.Row="1" Grid.Column="5" ItemsSource="{Binding}" SelectedItem="" />
        <ListView Grid.Row="1" Grid.Column="6" ItemsSource="{Binding}" SelectedItem="" />
        <ListView Grid.Row="1" Grid.Column="7" ItemsSource="{Binding}" SelectedItem="" />
    </Grid>
</UserControl>

I want the values ​​in the default array to show up on each TextBlock respectively, but when I run the program, nothing happens.

1 Answers
Related