私が書いているWPFアプリケーションでTabControlを作成しました。 TabItemを再テンプレートして、各タブヘッダーにボタンを閉じて閉じることができました。これまでのところ、すべてが順調です。WPF - ボタンコンテンツがボタン自体とは別に移動するのはなぜですか?
私は、デフォルトの四角い醜いものの代わりに光沢のある丸いボタンが欲しいと決めました。また、単純にコンテンツを「X」に設定するのではなく、画像をボタンコンテンツとして使用したいと思っていました。
私のXAMLスタイル/テンプレート:
<Style TargetType="{x:Type Button}" x:Key="EllipseButtonStyle">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Grid>
<Ellipse Fill="{TemplateBinding Background}"
Stroke="{TemplateBinding BorderBrush}"
StrokeThickness="{TemplateBinding BorderThickness}"
Width="{TemplateBinding Width}"
Height="{TemplateBinding Height}"/>
<ContentPresenter HorizontalAlignment="Center"
VerticalAlignment="Center"
Content="{TemplateBinding Button.Content}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<DataTemplate x:Key="ClosableTabItemTemplate">
<DockPanel MinWidth="120" Margin="0,0,0,0">
<ContentPresenter
Content="{Binding Path=DisplayName}"
VerticalAlignment="Center"
HorizontalAlignment="Left"/>
<Button
Command="{Binding Path=UnSubscribeApplicationCommand}"
CommandParameter="{Binding Path=DisplayName}"
BorderBrush="Black"
BorderThickness="2"
VerticalContentAlignment="Center"
VerticalAlignment="Center"
HorizontalAlignment="Right"
DockPanel.Dock="Right"
Width="16" Height="16">
<Image Source="closeicon.bmp" Height="8" Width="8"
VerticalAlignment="Center" HorizontalAlignment="Center"/>
<Button.Style>
<Style TargetType="{x:Type Button}"
BasedOn="{StaticResource EllipseButtonStyle}">
<Setter Property="Background"
Value="{StaticResource CloseOffButtonBrush}"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{StaticResource CloseOnButtonBrush}"/>
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
</DockPanel>
</DataTemplate>
代わりに上記のコードでは、しかし、選択したタブのコンテンツ(と同様の背景が)ので、私は前提とするもので上向きにシフトしているようだTabItemsコンテンツです選択されたために上に移動します。なぜ、楕円は他のコンテンツと一緒にシフトしていないのですか?誰でもここで何が起こっているか考えている?
なぜDataTemplateを使用していますか? もっとコードを投稿できますか? –
私は少し前にこの問題を解決しました。あなたはDataTemplateではないと思われます。私は少しだけ私のソリューションを投稿します。 – syazi