【问题标题】:Internal paddig of WPF EditableTextBlock not workingWPF 可编辑文本块的内部填充不起作用
【发布时间】:2013-03-07 20:29:37
【问题描述】:

我的 XAML 中有以下内容:

    <DataTemplate x:Key="ValueParserTemplate">
        <v:EditableTextBlock x:Name ="scalarEditorTextBox" Margin="17,3,0,0" Width="134" Height="29" 
                 FontFamily="Microsoft Sans Serif" FontSize="13"
                 Visibility="{Binding IsValueEditorVisible,Converter={StaticResource BoolToVisibility}}"
                 Text="{Binding Path=DisplayValue, UpdateSourceTrigger=Explicit, Mode=TwoWay,
                 ValidatesOnDataErrors=True,
                 ValidatesOnExceptions=True}"
                 Focusable="True"
                 TextAlignment="{Binding PxValueDefinition,Converter={StaticResource aignmentConverter}}"
                 ReadOnly="{Binding IsOutput, RelativeSource={RelativeSource AncestorType={x:Type vo:OutputTreeView}}}"
                 ScalarDataType="{Binding Path=PxValueDefinition}"
                 TextBlockBackgroundColor="{StaticResource ScalarBackground}"
                 TextBoxBackgroundColor="{StaticResource ScalarBackground}"
                 Background="{StaticResource ScalarBackground}"
                 BorderBrush ="Black" BorderThickness="1,1,1,1"/> 
    </DataTemplate>

我想为 TextBlock 的文本设置内部填充。 我尝试添加:

padding="5,5,5,5" -> no success
padding="50"      -> no success

没有任何作用。见:

你知道我怎样才能得到我想要的吗?

谢谢。


编辑: 啊...我想我必须在 EditableTextBlock 中添加填充! EditableTextBlock XAML:

 <Style TargetType="{x:Type v:EditableTextBlock}">
        <Setter Property="MinWidth" Value="134"/>
        <Setter Property="MinHeight" Value="16"/>
        <Setter Property="AllowDrop" Value="true"/>            
        <Setter Property="Validation.ErrorTemplate" Value="{x:Null}"/>
        <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type v:EditableTextBlock}">
                    <Border Name="Border" BorderBrush="{Binding BorderBrush, RelativeSource={RelativeSource AncestorType={x:Type v:EditableTextBlock}}}" BorderThickness="{Binding BorderThickness, RelativeSource={RelativeSource AncestorType={x:Type v:EditableTextBlock}}}" SnapsToDevicePixels="True">
                        <Grid x:Name="PART_GridContainer" Background="{TemplateBinding Background}" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}">
                            <TextBlock x:Name="PART_TbDisplayText" Visibility="Visible" 
                                       Background="{Binding TextBlockBackgroundColor, RelativeSource={RelativeSource AncestorType={x:Type v:EditableTextBlock}}}" 
                                       Foreground="{Binding TextBlockForegroundColor, RelativeSource={RelativeSource AncestorType={x:Type v:EditableTextBlock}}}" 
                                       Text="{Binding Text, RelativeSource={RelativeSource AncestorType={x:Type v:EditableTextBlock}}, Mode=TwoWay}" 
                                       VerticalAlignment="Center" Padding="0,0,3,0"/>
                            <TextBox x:Name="PART_TbEditText" 
                                     Visibility="Hidden" 
                                     Background="{Binding TextBoxBackgroundColor, RelativeSource={RelativeSource AncestorType={x:Type v:EditableTextBlock}}}" 
                                     Foreground="{Binding TextBoxForegroundColor, RelativeSource={RelativeSource AncestorType={x:Type v:EditableTextBlock}}}" 
                                     Text="{Binding Text, RelativeSource={RelativeSource AncestorType={x:Type v:EditableTextBlock}}, Mode=TwoWay}" 
                                     BorderThickness="0" VerticalAlignment="Center"/>
                        </Grid>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter TargetName="Border" Property="Background" Value="LightGray"/>
                            <Setter TargetName="Border" Property="BorderBrush" Value="Black"/>
                            <Setter Property="Foreground" Value="Gray"/>
                        </Trigger>
                        <Trigger Property="Validation.HasError" Value="true">
                            <Setter TargetName="Border" Property="BorderBrush" 
                            Value="Red"/>
                        </Trigger>
                        <Trigger Property="IsFocused" Value="True">
                            <Setter Property="ShowFocusVisual" Value="True"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="Validation.HasError" Value="true">
                <Setter Property="ToolTip"
                    Value="{Binding RelativeSource={x:Static RelativeSource.Self},
                    Path=(Validation.Errors).CurrentItem.ErrorContent}"/>
            </Trigger>
        </Style.Triggers>
    </Style>

【问题讨论】:

  • 我猜你的 EditableTextBlock 是一个用户控件,对吧?你可以发布它的xaml吗?
  • 编辑了问题,并添加了 EditableTextBlock XAML

标签: wpf padding textblock


【解决方案1】:

您应该在模板中将 TextBlock 和 TextBox 的边距设置为绑定到其父级的 Padding 属性:

    <Border Name="Border" BorderBrush="{Binding BorderBrush, RelativeSource={RelativeSource AncestorType={x:Type v:EditableTextBlock}}}" BorderThickness="{Binding BorderThickness, RelativeSource={RelativeSource AncestorType={x:Type v:EditableTextBlock}}}" SnapsToDevicePixels="True">
        <Grid x:Name="PART_GridContainer" Background="{TemplateBinding Background}" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}">
            <TextBlock x:Name="PART_TbDisplayText" Visibility="Visible"
                                   Margin="{TemplateBinding Padding}"
                                   Background="{Binding TextBlockBackgroundColor, RelativeSource={RelativeSource AncestorType={x:Type v:EditableTextBlock}}}" 
                                   Foreground="{Binding TextBlockForegroundColor, RelativeSource={RelativeSource AncestorType={x:Type v:EditableTextBlock}}}" 
                                   Text="{Binding Text, RelativeSource={RelativeSource AncestorType={x:Type v:EditableTextBlock}}, Mode=TwoWay}" 
                                   VerticalAlignment="Center" Padding="0,0,3,0"/>
            <TextBox x:Name="PART_TbEditText" 
                                 Visibility="Hidden" 
                                 Margin="{TemplateBinding Padding}"
                                 Background="{Binding TextBoxBackgroundColor, RelativeSource={RelativeSource AncestorType={x:Type v:EditableTextBlock}}}" 
                                 Foreground="{Binding TextBoxForegroundColor, RelativeSource={RelativeSource AncestorType={x:Type v:EditableTextBlock}}}" 
                                 Text="{Binding Text, RelativeSource={RelativeSource AncestorType={x:Type v:EditableTextBlock}}, Mode=TwoWay}" 
                                 BorderThickness="0" VerticalAlignment="Center"/>
        </Grid>
    </Border>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-11
    • 2014-04-14
    • 1970-01-01
    • 2015-07-10
    • 1970-01-01
    • 2013-01-14
    相关资源
    最近更新 更多