我有一个自定义控件,我试图扩展Track类。
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>
但它给了我&#34; Collection属性&#39; CustomCollectionControl.MultiThumbTrack&#39;。&#39; ThumbsList&#39;是空的。&#34;我尝试这样做时出错:
<local:MultiThumb
答案 0 :(得分:4)
在实例构造函数中初始化ThumbsList属性:
public class MultiThumbTrack : Track
{
static MultiThumbTrack()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MultiThumbTrack),
new FrameworkPropertyMetadata(typeof(MultiThumbTrack)));
}
public MultiThumbTrack()
{
ThumbsList = new List<Thumb>();
}
...
}