我有一个自定义控件NumericUpDown。我创建了一个命名样式,我想在其中扩展控件,在这种情况下,将它包装在Grid中并添加Label。
当我使用按钮作为基本示例时,我可以很好地扩展ControlTemplate。原始Button显示在ContentPresenter的位置,我可以在其周围添加其他控件以进行布局等等。
ControlTemplate TargetType="{x:Type Button}">
<Border BorderThickness="3" BorderBrush="Red" Background="{TemplateBinding Background}">
<ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Border>
</ControlTemplate>
然而,当使用我的自定义控件时,ContentPresenter不显示NumericUpDown控件,它绝对没有显示任何内容。即使是最基本的形式,我也无法让ContentPresenter显示自定义控件。
<Style x:Key="LabeledNumericUpDown2" TargetType="{x:Type controls:NumericUpDown}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type controls:NumericUpDown}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="5*"/>
<ColumnDefinition Width="5*"/>
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Content="Blabla"/>
<ContentPresenter Grid.Column="1"/>
<!--<controls:NumericUpDown Grid.Column="1"/>-->
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我不能为我的生活找出我所缺少的东西。
感谢您的帮助,非常感谢!
修改
NumericUpDown自定义控件还具有定义NumericUpDown控件的默认样式。我希望以命名的风格延伸到这一点。
<Style TargetType="{x:Type controls:NumericUpDown}">
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="VerticalAlignment" Value="Top" />
<Setter Property="Margin" Value="0" />
<Setter Property="Height" Value="18" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type controls:NumericUpDown}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="16" />
</Grid.ColumnDefinitions>
<TextBox x:Name="PART_TextBox" Grid.Column="0" HorizontalAlignment="Stretch" VerticalAlignment="Top" Padding="0" Margin="0"
Text="{Binding DisplayValue, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}" />
<Border Grid.Column="1" BorderThickness="0 1 1 1" BorderBrush="#FFABADB3">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="8" />
<RowDefinition Height="8" />
</Grid.RowDefinitions>
<Button x:Name="PART_ButtonUp" Grid.Row="0" Content="▲" FontSize="4" HorizontalContentAlignment="Left" VerticalContentAlignment="Top" BorderThickness="0" Padding="6 1 0 0" />
<Button x:Name="PART_ButtonDown" Grid.Row="1" Content="▼" FontSize="4" HorizontalContentAlignment="Left" VerticalContentAlignment="Top" BorderThickness="0" Padding="6 1 0 0" />
</Grid>
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
答案 0 :(得分:0)
某些Controls
定义并期望特定的命名元素正常运行,否则称为部件。
确保ControlTemplate
已为Control
提供了所需的部分,以便正确呈现。
修改强>
试试这个ControlTemplate
让您入门。
<ControlTemplate TargetType="{x:Type controls:NumericUpDown}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="5*"/>
<ColumnDefinition Width="5*"/>
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Content="Blabla"/>
<Grid Grid.Column="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="16" />
</Grid.ColumnDefinitions>
<TextBox x:Name="PART_TextBox" Grid.Column="0" HorizontalAlignment="Stretch" VerticalAlignment="Top" Padding="0" Margin="0"
Text="{Binding DisplayValue, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}" />
<Border Grid.Column="1" BorderThickness="0 1 1 1" BorderBrush="#FFABADB3">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="8" />
<RowDefinition Height="8" />
</Grid.RowDefinitions>
<Button x:Name="PART_ButtonUp" Grid.Row="0" Content="▲" FontSize="4" HorizontalContentAlignment="Left" VerticalContentAlignment="Top" BorderThickness="0" Padding="6 1 0 0" />
<Button x:Name="PART_ButtonDown" Grid.Row="1" Content="▼" FontSize="4" HorizontalContentAlignment="Left" VerticalContentAlignment="Top" BorderThickness="0" Padding="6 1 0 0" />
</Grid>
</Border>
</Grid>
</Grid>
</ControlTemplate>