当设置 PropertyMetadata 中的默认值是字符串数组时无法获取项目的值
Cannot get value of item when set Default value in PropertyMetadata is Array of string
我的自定义控件中有一个 DependencyProperty
。我已将 DayOfWeeksProperty
的默认值设置为字符串值数组,然后将 PropertyChangedCallback
设置为一种方法,其中 TextBlock
是默认数组中每个元素的设置值。
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>
我希望默认数组中的值分别显示在每个TextBlock
上,但是当我运行程序时,没有任何反应
我已经在Constructor中为它设置了默认值,它起作用了。
public ScheduleControl()
{
InitializeComponent();
string[] defaultDayOfWeeks =
{
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday",
};
SetValue(DayOfWeeksProperty, defaultDayOfWeeks);
}
我的自定义控件中有一个 DependencyProperty
。我已将 DayOfWeeksProperty
的默认值设置为字符串值数组,然后将 PropertyChangedCallback
设置为一种方法,其中 TextBlock
是默认数组中每个元素的设置值。
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>
我希望默认数组中的值分别显示在每个TextBlock
上,但是当我运行程序时,没有任何反应
我已经在Constructor中为它设置了默认值,它起作用了。
public ScheduleControl()
{
InitializeComponent();
string[] defaultDayOfWeeks =
{
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday",
};
SetValue(DayOfWeeksProperty, defaultDayOfWeeks);
}