首页 文章

为什么我的Window-wide TextBox样式不适用?

提问于
浏览
0

我有一个窗口wpf应用程序,并尝试设置一个窗口范围的样式,以应用于我的所有TextBox控件 . 我有另一种样式在TextBlock控件上按预期工作,但由于某种原因,TextBox样式仅在我使用x:Key时才起作用 . 我试图让这个样式成为我窗口中所有TextBox的全局 .

我应该注意到我在TextBox样式上尝试了多个属性,包括边框粗细,前景,背景,TextWeight等,除非键入为每个TextBox控件显式定义样式,否则无效 .

这段代码目前在我的App.xaml中,但我也在Window.Resources下测试了它 .

<ResourceDictionary>
    <vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" xmlns:vm="clr-namespace:GraphicalNestingCalculator.ViewModel" />
    <Style TargetType="{x:Type TextBlock}">
        <Setter Property="Foreground" Value="Blue"/>
    </Style>
    <Style x:Key="TextBoxStyle" TargetType="{x:Type TextBox}">
        <Setter Property="Background" Value="Red"/>
    </Style>
</ResourceDictionary>

而堆栈面板中的TextBox

<StackPanel Orientation="Horizontal">
            <StackPanel.Resources>
                <Style TargetType="{x:Type TextBox}">
                    <Setter Property="Margin" Value="10,0,10,0"/>
                </Style>
            </StackPanel.Resources>
            <TextBlock>Part Width:</TextBlock>
            <TextBox Name="partWidthTextBox" Text="{Binding Path=Layout.Part.Width, UpdateSourceTrigger=LostFocus}" Style="{StaticResource TextBoxStyle}" Width="50" >
            </TextBox>
            <TextBlock>Part Height:</TextBlock>
            <TextBox Name="partHeightTextBox" Text="{Binding Path=Layout.Part.Height, UpdateSourceTrigger=LostFocus}" Style="{StaticResource TextBoxStyle}" Width="50" />
        </StackPanel>

2 回答

  • 0

    如果你想要的是没有x:Key的隐式Style和你在App.xaml或_2655603中定义的样式,你应该将前者放在后者上:

    <StackPanel Orientation="Horizontal">
        <StackPanel.Resources>
            <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
                <Setter Property="Margin" Value="10,0,10,0"/>
                <Setter Property="FontSize" Value="40" />
            </Style>
        </StackPanel.Resources>
        <TextBlock>Part Width:</TextBlock>
        <TextBox Name="partWidthTextBox" Text="{Binding Path=Layout.Part.Width, UpdateSourceTrigger=LostFocus}" Width="50" >
        </TextBox>
        <TextBlock>Part Height:</TextBlock>
        <TextBox Name="partHeightTextBox" Text="{Binding Path=Layout.Part.Height, UpdateSourceTrigger=LostFocus}" Width="50" />
    </StackPanel>
    

    App.xaml:

    <ResourceDictionary>
        <vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" xmlns:vm="clr-namespace:GraphicalNestingCalculator.ViewModel" />
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="Foreground" Value="Blue"/>
        </Style>
        <Style TargetType="{x:Type TextBox}">
            <Setter Property="Background" Value="Red"/>
        </Style>
    </ResourceDictionary>
    

    在TextBox上设置 Style="{StaticResource TextBoxStyle}" 时,将忽略在 <StackPanel.Resources> 中定义的隐式样式 . 这是预期的行为 .

  • 1

    在定义窗口范围的资源样式时必须删除密钥,否则稍后需要显式引用它

    </Style>
    <Style TargetType="TextBox">
        <Setter Property="Background" Value="Red"/>
    </Style>
    

    编辑

    如果您还要应用更具体的样式,则更具体的样式应基于窗口宽度的样式: BasedOn="{StaticResource {x:Type TextBox}}" (已在其他答案中显示)

    无论如何,因为为StackPanel子元素设置边距的要求可能比仅对TextBox更通用,所以还有另一个选项attached property而不是样式

    <StackPanel Orientation="Horizontal" local:MarginSetter.Margin="10">
    

    边距的最后一个通用解决方案更复杂但是 - 对于要加载的面板的子节点 - 我订阅了以下事件

    private void StackPanel_Loaded(object sender, RoutedEventArgs e)
        {
            MarginSetter.CreateThicknesForChildren(sender, new DependencyPropertyChangedEventArgs());
        }
    

相关问题