gpt4 book ai didi

c# - 如何在 WPF 中将 CellTemplate 中的 TextBlock 居中?

转载 作者:行者123 更新时间:2023-11-30 13:50:56 27 4
gpt4 key购买 nike

我尝试了 Horizo​​ntalAlignmentTextAlignment 但没有效果。

<GridViewColumn Width="Auto" Header="Type">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Type}" HorizontalAlignment="Center" TextAlignment="Center" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>

有什么想法吗?

编辑:

Xaml 代码:

<Window x:Class="EffectsWindow.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Effects Editor"
<DockPanel VerticalAlignment="Stretch">
<DockPanel.Resources>

<ListView
DockPanel.Dock="Top"
HorizontalContentAlignment="Stretch"
ItemsSource="{Binding EditorViewModel.AllEffects}">

<ListView.View>
<GridView>
<GridViewColumn Width="Auto"
DisplayMemberBinding="{Binding Name}"
Header="Name" />

<GridViewColumn Width="Auto" Header="Type">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Type}" HorizontalAlignment="Center" TextAlignment="Center" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>

</GridView>
</ListView.View>
</ListView>

<DockPanel Width="100"
Height="100"
Margin="0,0,20,20"
HorizontalAlignment="Right"
VerticalAlignment="Bottom">

<Label Margin="0,0,0,0"
Content="Data"
DockPanel.Dock="Top" />

<ListBox
VerticalAlignment="Top"/>

</DockPanel>

</DockPanel>
</Window>

最佳答案

您尝试过 Horizo​​ntalContentAlignment 吗?在专栏上?

-- 编辑--

事实证明很难找到放置此 Horizo​​ntalContentAlignment 的正确位置...:)

用这个

<ListView.Resources>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Center" />
</Style>
</ListView.Resources>

在 ListView 中。

Snoop 通常可以帮助您走上正确的道路。这个 ContentPresenter 是 ListViewItem 使用的,它从 ListViewItem 上的 ContentAlignment 获取它的 Alignment。你不能直接访问 ListViewItem,这就是你需要那种样式的原因

和完整代码:

<DockPanel VerticalAlignment="Stretch">

<ListView
DockPanel.Dock="Top"
HorizontalContentAlignment="Stretch"
x:Name="MyListView">
<ListView.Resources>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Center" />
</Style>

</ListView.Resources>
<ListView.View>
<GridView >

<GridViewColumn Width="Auto"
DisplayMemberBinding="{Binding Name}"
Header="Name" />

<GridViewColumn Width="Auto" Header="Type">

<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Type}" HorizontalAlignment="Center" TextAlignment="Center" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>

</GridView>
</ListView.View>
</ListView>

<DockPanel Width="100"
Height="100"
Margin="0,0,20,20"
HorizontalAlignment="Right"
VerticalAlignment="Bottom">

<Label Margin="0,0,0,0"
Content="Data"
DockPanel.Dock="Top" />

<ListBox
VerticalAlignment="Top"/>

</DockPanel>

</DockPanel>

关于c# - 如何在 WPF 中将 CellTemplate 中的 TextBlock 居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5332738/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com