集合 属性 为空。自定义控件依赖属性
Collection property is null. Custom control dependency properties
我有一个自定义控件,我正在尝试扩展轨道 class。
public class MultiThumbTrack : Track
{tyMetadata(typeof(MultiThumbTrack)));
}
public List<Thumb> ThumbsList
{
get { return (List<Thumb>)GetValue(ThumbsListProperty); }
set { SetValue(ThumbsListProperty, value); }
}
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto" MinHeight="{TemplateBinding MinHeight}"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TickBar x:Name="TopTick" Fill="{TemplateBinding Foreground}" Height="4" Margin="0,0,0,2" Placement="Top" Grid.Row="0" Visibility="Collapsed"/>
<TickBar x:Name="BottomTick" Fill="{TemplateBinding Foreground}" Height="4" Margin="0,2,0,0" Placement="Bottom" Grid.Row="2" Visibility="Collapsednd}" Height="4.0" Margin="5,0" Grid.Row="1" VerticalAlignment="center">
<Canvas Margin="-6,-1">
<Rectangle x:Name="PART_SelectionRange" Fill="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" Height="4.0" Visibility="Hidden"/>
</Canvas>
</Border>
<local:MultiThumbTrack x:Name="PART_Track" Grid.Row="1">
<local:MultiThumbTrack.DecreaseRepeatButton>
<RepeatButton Command="{x:Static Slider.DecreaseLarge}" Style="{StaticResource RepeatButtonTransparent}"/>
</local:MultiThumbTrack.DecreaseRepeatButton>
<local:MultiThumbTrack.IncreaseRepeatButton>
<RepeatButton Command="{x:Static Slider.IncreaseLarge}" Style="{StaticResource RepeatButtonTransparent}"/>
</local:MultiThumbTrack.IncreaseRepeatButton>
<!--<local:MultiThumbTrack.Thumb>
"Center" />
</local:MultiThumbTrack.Thumb>-->
<local:MultiThumbTrack.ThumbsList>
<Thumb Margin="1,15,0,0" x:Name="Thumb" Focusable="False" Height="13" Width="9" OverridesDefaultStyle="True" Template="{StaticResource SliderThumbHorizontalDefault}" VerticalAlignment="Center" />
</local:MultiThumbTrack.ThumbsList>
</local:MultiThumbTrack>
</Grid>
</Border>
</ControlTemplate>
但是当我尝试这样做时它给了我 "Collection property 'CustomCollectionControl.MultiThumbTrack'.'ThumbsList' is null." 错误:
<local:MultiThumb
在实例构造函数中初始化 ThumbsList 属性:
public class MultiThumbTrack : Track
{
static MultiThumbTrack()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MultiThumbTrack),
new FrameworkPropertyMetadata(typeof(MultiThumbTrack)));
}
public MultiThumbTrack()
{
ThumbsList = new List<Thumb>();
}
...
}
我有一个自定义控件,我正在尝试扩展轨道 class。
public class MultiThumbTrack : Track
{tyMetadata(typeof(MultiThumbTrack)));
}
public List<Thumb> ThumbsList
{
get { return (List<Thumb>)GetValue(ThumbsListProperty); }
set { SetValue(ThumbsListProperty, value); }
}
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto" MinHeight="{TemplateBinding MinHeight}"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TickBar x:Name="TopTick" Fill="{TemplateBinding Foreground}" Height="4" Margin="0,0,0,2" Placement="Top" Grid.Row="0" Visibility="Collapsed"/>
<TickBar x:Name="BottomTick" Fill="{TemplateBinding Foreground}" Height="4" Margin="0,2,0,0" Placement="Bottom" Grid.Row="2" Visibility="Collapsednd}" Height="4.0" Margin="5,0" Grid.Row="1" VerticalAlignment="center">
<Canvas Margin="-6,-1">
<Rectangle x:Name="PART_SelectionRange" Fill="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" Height="4.0" Visibility="Hidden"/>
</Canvas>
</Border>
<local:MultiThumbTrack x:Name="PART_Track" Grid.Row="1">
<local:MultiThumbTrack.DecreaseRepeatButton>
<RepeatButton Command="{x:Static Slider.DecreaseLarge}" Style="{StaticResource RepeatButtonTransparent}"/>
</local:MultiThumbTrack.DecreaseRepeatButton>
<local:MultiThumbTrack.IncreaseRepeatButton>
<RepeatButton Command="{x:Static Slider.IncreaseLarge}" Style="{StaticResource RepeatButtonTransparent}"/>
</local:MultiThumbTrack.IncreaseRepeatButton>
<!--<local:MultiThumbTrack.Thumb>
"Center" />
</local:MultiThumbTrack.Thumb>-->
<local:MultiThumbTrack.ThumbsList>
<Thumb Margin="1,15,0,0" x:Name="Thumb" Focusable="False" Height="13" Width="9" OverridesDefaultStyle="True" Template="{StaticResource SliderThumbHorizontalDefault}" VerticalAlignment="Center" />
</local:MultiThumbTrack.ThumbsList>
</local:MultiThumbTrack>
</Grid>
</Border>
</ControlTemplate>
但是当我尝试这样做时它给了我 "Collection property 'CustomCollectionControl.MultiThumbTrack'.'ThumbsList' is null." 错误:
<local:MultiThumb
在实例构造函数中初始化 ThumbsList 属性:
public class MultiThumbTrack : Track
{
static MultiThumbTrack()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MultiThumbTrack),
new FrameworkPropertyMetadata(typeof(MultiThumbTrack)));
}
public MultiThumbTrack()
{
ThumbsList = new List<Thumb>();
}
...
}