wpf - WPF另一个元素中间的中心元素

标签 wpf mvvm binding element

我想通过TextBoxLine( WPF )创建一种标题。

<Grid>
    <Grid.Resources>
        <Style x:Key="labelTextbox" TargetType="{x:Type TextBox}" >
            <Setter Property="IsReadOnly" Value="True"/>
            <Setter Property="HorizontalAlignment" Value="Left" />
            <Setter Property="BorderThickness" Value="0"/>
            <Setter Property="Margin" Value="10,0,0,0"/>
            <Setter Property="Padding" Value="4,1,5,1"/>
        </Style>
    </Grid.Resources>
    <Grid.RowDefinitions>
        <RowDefinition Height="20"/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <Line  X1="5" X2="5000" VerticalAlignment="Center" StrokeThickness="1" Stroke="Gray"/>
    <TextBox Text="Buttons" Style="{StaticResource labelTextbox}"/>
    <Grid Grid.Row="1">
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Button Margin="10" Content="Button A"/>
        <Button Margin="10" Content="Button B" Grid.Row="1"/>
        <Button Margin="10" Content="Button C" Grid.Row="2"/>
        <Button Margin="10" Content="Button D" Grid.Row="3"/>
    </Grid>
</Grid>

屏幕看起来像这样:

如您所见,该行与网格的中间行对齐,但未与文本框的中间对齐。

我试图将文本框VerticalAlignment="Center"放进去,并且试图删除边距和填充,但是没有任何帮助。

如何在不通过位置专门定义的情况下将行居中到文本框的中间?(一般来说,它可以适合任何大小的行和任何大小的文本)

最佳答案

尝试Line设置 UseLayoutRounding="True" :

Gets or sets a value that determines whether rendering for the object and its visual subtree should use rounding behavior that aligns rendering to whole pixels.



Example
<Line UseLayoutRounding="True"
      X1="5" X2="5000" ... />

Output
UseLayoutRounding="False"UseLayoutRounding="True"
在这种情况下,UseLayoutRounding在中间形成一个像素行。如果要使Line划掉文本,则需要交换TextBox和Line,但在这种情况下,还需要使用UseLayoutRounding="True"

关于wpf - WPF另一个元素中间的中心元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22754061/

相关文章:

c# - 如何将 WPF CheckBox 绑定(bind)到条件?

.net - 使用样式将控件包裹在边框中而不覆盖默认外观

c# - WPF - 需要在禁用的 DataGrid 中启用滚动

c# - WPF 动画性能随时间下降

c# - DataContext 和 BindingContext 有什么区别

.net - 在ListView中同步IsSelected和SelectedItem

c# - 使用 MVVM 在 DataGrid 中级联 ComboBox

javascript - Kendo Tabstrip 使用 MVVM 在左侧显示选项卡

WPF 绑定(bind)验证 - ComboBox SelectedItem 将无法验证

android - 崩溃后重启无障碍服务