0

デフォルトの wpf MenuItem (メニュー上) は、およそのコントロールから構築されます。このように:
グリッド; 外側の長方形; bg-長方形; 内側の長方形; ドックパネル; 現れる。

ドックパネルは次のもので構成されてい
ます。道; コンテンツプレゼンター[テキスト]

contentpresenter [text]TextBlockコントロールで構成されます。

私が達成したいのはStyle、可能な限り単純にa を定義して、 VerticalAlignmentthis のプロパティを変更することですが、 inTextBlockに対してのみであり、一般的にはそうではありません。TextBlockMenuItem

<Style x:Key ="TextBlockCenterStyle" TargetType="{x:Type TextBlock}">
        <Setter Property="VerticalAlignment" Value="Center" />
    </Style>

<Style TargetType="MenuItem">
        <Setter Property="FontSize" Value="11" />
        <Setter Property="ItemContainerStyle" Value="TextBlockCenterStyle" />
        <Style.Resources>
            <Style TargetType="{x:Type TextBlock}">
                <Setter Property="VerticalAlignment" Value="Center" />
            </Style>
      </Style.Resources>
</Style>

私は試しStyle.ResourcesてみItemContainerStyleました。
動作させることができません。実行時に(から) をItemContainerStyleスローします。 可能であれば、FindChildControl?! のような一般的なソリューションにする必要があります。TargetInvocationExceptionNullReferenceException

4

1 に答える 1

0

ItemContainerStyle を試しましたか?

何かのようなもの:

 <MenuItem ItemContainerStyle = {StaticResource MyItemContainerStyle}../>

次に、 MyItemContainerStyle にはあなたの

 <Style x:Key ="MyItemContainerStyle" TargetType="{x:Type TextBlock}">
                <Setter Property="VerticalAlignment" Value="Center" />
            </Style>

===============編集後の回答======================これを試してください:

<Style TargetType="MenuItem">
        <Setter Property="FontSize" Value="11" />
        <Setter Property="ItemContainerStyle" Value="{StaticResource TextBlockCenterStyle}" />
        <Style.Resources>
            <Style TargetType="{x:Type TextBlock}">
                <Setter Property="VerticalAlignment" Value="Center" />
            </Style>
      </Style.Resources>
</Style>
于 2013-10-04T16:01:13.780 に答える