XAML C# Foreground / Background TemplateBinding未应用于初始设置器,但应用于触发器

mspsb9vt  于 2022-12-07  发布在  C#
关注(0)|答案(1)|浏览(187)

我尝试在初始设置器中设置自定义控件中TextBlock的前景,但颜色仍然是默认的。奇怪的是在Trigger中这样做,它工作得很好。
我还将TemplateBinding添加到自定义控件中的TextBlock.Foreground属性,但这并没有什么不同。
MainWindow.xaml:

<controls:NavigationDrawer
    Grid.Column="0"
    Background="#212121"
    IsOpen="{Binding IsChecked, ElementName=cbToggleNavigationDrawer}"
    OpenCloseDuration="0:0:0.25">

    <controls:NavigationDrawer.Resources>
        <Style TargetType="controls:NavigationDrawerItem">
            <!-- does not work -->
            <Setter Property="Foreground" Value="White" />
            <Setter Property="ImageSize" Value="50" />
            <Setter Property="Margin" Value="0,20,0,0" />
            <Style.Triggers>
                <Trigger Property="IsChecked" Value="True">
                    <Setter Property="FontWeight" Value="SemiBold" />
                    <!-- works perfectly fine -->
                    <Setter Property="Foreground" Value="White" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </controls:NavigationDrawer.Resources>

    <controls:NavigationDrawer.Content>
        <StackPanel Margin="10,10">
            <controls:NavigationDrawerItem Content="Home" ImageSource="/Resources/home-white.png" />
            <controls:NavigationDrawerItem Content="Launchers" ImageSource="/Resources/launcher-white.png" />
            <controls:NavigationDrawerItem Content="Games" ImageSource="/Resources/game-white.png" />
            <controls:NavigationDrawerItem Content="Contribute" ImageSource="/Resources/contribute-white.png" />
            <controls:NavigationDrawerItem Content="About" ImageSource="/Resources/about-white.png" />
        </StackPanel>
    </controls:NavigationDrawer.Content>
</controls:NavigationDrawer>

NavigationDrawerItemStyle.xaml:

<Style TargetType="{x:Type controls:NavigationDrawerItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type controls:NavigationDrawerItem}">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="auto" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>

                    <Viewbox
                        Grid.Column="0"
                        Width="{TemplateBinding ImageSize}"
                        Height="{TemplateBinding ImageSize}">
                        <Image Source="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ImageSource}" />
                    </Viewbox>

                    <TextBlock
                        Grid.Column="1"
                        Margin="10,0,0,0"
                        VerticalAlignment="Center"
                        Text="{TemplateBinding Content}" />

                    <!--  
                    Does not make a difference to add 
                    Foreground="{TemplateBinding Foreground}"
                    to TextBlock
                    -->

                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

这是启动应用程序后的外观:

ejk8hzay

ejk8hzay1#

尝试将触发器移动到ControlTemplate,并直接设置TextBlockForeground属性:

<ControlTemplate TargetType="{x:Type controls:NavigationDrawerItem}">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="auto" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>

        <Viewbox
            Grid.Column="0"
            Width="{TemplateBinding ImageSize}"
            Height="{TemplateBinding ImageSize}">
            <Image Source="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ImageSource}" />
        </Viewbox>

        <TextBlock x:Name="tb"
            Grid.Column="1"
            Margin="10,0,0,0"
            VerticalAlignment="Center"
            Foreground="White"
            Text="{TemplateBinding Content}" />

    </Grid>
    <ControlTemplate.Triggers>
        <Trigger Property="IsSelected" Value="True">
            <Setter TargetName="tb" Property="Foreground" Value="White" />
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

如果这样做不起作用,则可能是在代码的其他位置设置了Foreground属性。

相关问题