Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在GridViewColumn中居中放置文本块?(包括样本xaml)_C#_.net_Wpf_Xaml_Layout - Fatal编程技术网

C# 如何在GridViewColumn中居中放置文本块?(包括样本xaml)

C# 如何在GridViewColumn中居中放置文本块?(包括样本xaml),c#,.net,wpf,xaml,layout,C#,.net,Wpf,Xaml,Layout,Effect只是一个类型,它有一个返回字符串的Name属性,但文本仍然没有在其单元格内居中 关于如何修复它有什么想法吗?您可以为ItemContainerStyle扩展HorizontalContentAlignment <Window x:Class="EffectsWindow.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="ht

Effect
只是一个类型,它有一个返回字符串的Name属性,但文本仍然没有在其单元格内居中


关于如何修复它有什么想法吗?

您可以为
ItemContainerStyle
扩展
HorizontalContentAlignment

<Window x:Class="EffectsWindow.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        DataContext="{Binding RelativeSource={RelativeSource Self}}">

    <ListView ItemsSource="{Binding EffectsViewModel.Effects}">
        <ListView.View>
            <GridView>
                <GridViewColumn Width="200" Header="Effects">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock HorizontalAlignment="Center"
                                       Text="{Binding Name}"
                                       TextAlignment="Center" />
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>

</Window>

尝试使用一个应用程序,如目视检查应用程序的属性,您可以看到哪里没有像预期的那样对齐。

谢谢,我这样做了,但仍然没有帮助我找到导致它不居中的原因。谢谢,但代码将所有ListViewItems居中,对吗?我只想让一些列居中,另一些列保持原样(向左对齐)。@Joan Venge:它将使您设置的列居中
HorizontalAlignment=“center”
,否则它将左对齐(或您指定的任何内容)Meleak,这样就行了。谢谢,但是有个小问题。基本上,我使用这种交替颜色:第一个xaml示例,它也设置了样式,所以它们似乎不能一起工作。有可能把这些结合起来吗?@Joan Venge:更新了我的答案。如果我正确理解了你的问题,那应该行得通谢谢Meleak,效果很好。我想看看在现有的交替样式中,我可以在哪里安装您的代码,但做不到。感谢您展示如何做到这一点。
<ListView ItemsSource="{Binding EffectsViewModel.Effects}">
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
        </Style>
    </ListView.ItemContainerStyle>
    <!--...-->
</ListView>
<ListView ItemsSource="{Binding EffectsViewModel.Effects}"
          AlternationCount="2">
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
            <Style.Triggers>
                <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                    <Setter Property="Background" Value="LightBlue"></Setter>
                </Trigger>
                <Trigger Property="ItemsControl.AlternationIndex" Value="2">
                    <Setter Property="Background" Value="LightGray"></Setter>
                </Trigger>
            </Style.Triggers>
        </Style>
    </ListView.ItemContainerStyle>
    <!--...-->
</ListView>