C# 选择“中心控制/柱”

C# 选择“中心控制/柱”,c#,wpf,C#,Wpf,我有一个带有多个控件的网格,我想选择这些控件中的哪一个(或网格列中的哪一个)将位于中心,然后将其他列/控件放置在它旁边。我该怎么做?更新: <Grid ShowGridLines="True"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"></ColumnDefinition> <ColumnDefinition Width="Aut

我有一个带有多个控件的网格,我想选择这些控件中的哪一个(或网格列中的哪一个)将位于中心,然后将其他列/控件放置在它旁边。我该怎么做?

更新:

    <Grid ShowGridLines="True">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"></ColumnDefinition>
        <ColumnDefinition Width="Auto"></ColumnDefinition>
        <ColumnDefinition Width="*"></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="4*"></RowDefinition>
        <RowDefinition Height="Auto"></RowDefinition>
        <RowDefinition Height="*"></RowDefinition>
    </Grid.RowDefinitions>
    <StackPanel Grid.Row="1" x:Name="TheLeftComponents" Orientation="Horizontal" HorizontalAlignment="Right" >
    <Ellipse  Height="20" Width="20" Stroke="Black" StrokeThickness="1" ></Ellipse>
    </StackPanel>
    <Ellipse x:Name="TheMiddleComponent" Grid.Row="1" Grid.Column="1"  Height="40" Width="40" Stroke="Black" StrokeThickness="1" ></Ellipse>
    <StackPanel Grid.Row="1" Grid.Column="2" x:Name="TheRightComponents" Orientation="Horizontal" HorizontalAlignment="Left" >
        <Ellipse  Height="20" Width="20" Stroke="Black" StrokeThickness="1" ></Ellipse>
        <Ellipse  Height="20" Width="20" Stroke="Black" StrokeThickness="1" ></Ellipse>
        <Ellipse  Height="20" Width="20" Stroke="Black" StrokeThickness="1" ></Ellipse>
        <Ellipse  Height="20" Width="20" Stroke="Black" StrokeThickness="1" ></Ellipse>
        <Ellipse  Height="20" Width="20" Stroke="Black" StrokeThickness="1" ></Ellipse>
    </StackPanel>
</Grid>
以下模式:

    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="MainControl's height"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="MainControl's width"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
将通过以下方式定位控件:


在布局方面,您想要实现什么目标?您的网格策略将决定整个布局的行为,特别是当您希望它根据窗口大小进行缩放时。如果您想让布局对窗口大小做出反应,那么可以使用“自动”和“星形调整”策略。如果您实际上不关心调整大小,并且希望有固定的布局,则可以跨列共享网格大小

场景1- 如果您试图完全实现一个包含未知大小对象的中间列,并且该列必须完全居中,那么您需要用一个左右列缓冲该中间列,如Bahman_Aries的示例所示

<Grid>
     <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
</Grid>

结果是,放置在中间列中的项目将消耗尽可能多的宽度(所需的渲染宽度),而剩下的两列将在剩下的空间中分裂,因为每个值都有1*的值。换句话说,如果中间列对象请求200宽度,而窗口的总宽度为1000,则左侧和右侧列的宽度都将为400(800/2)

请注意,缩小窗口大小时,星形列将裁剪内容。这是故意的。如果你想让你的比率持续下去,你可以考虑在视图框(或整个网格)中包装布局。

场景2-如果您正在寻找一个不考虑缩放的网格来布局内容(re:中间的列消耗它所需要的内容,然后左列和右列在有大内容时平均增长),那么您可以使用SharedSizeScope功能

 <Grid IsSharedSizeScope="True">
         <Grid.ColumnDefinitions>
            <ColumnDefinition SharedSizeGroup="MyRatio"/>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="Auto" SharedSizeGroup="MyRatio"/>
        </Grid.ColumnDefinitions>
    </Grid>

这将实现的是,每当列2增长时,它也会将其大小传播到列0,从而确保列1始终居中

与场景1的主要区别在于,场景2将根据其子元素具有固定的大小,而场景1具有根据其父容器行为的结构

编辑:场景1,带有一个明确的示例:

    <Grid ShowGridLines="True">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"></ColumnDefinition>
        <ColumnDefinition Width="Auto"></ColumnDefinition>
        <ColumnDefinition Width="*"></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="4*"></RowDefinition>
        <RowDefinition Height="Auto"></RowDefinition>
        <RowDefinition Height="*"></RowDefinition>
    </Grid.RowDefinitions>
    <StackPanel Grid.Row="1" x:Name="TheLeftComponents" Orientation="Horizontal" HorizontalAlignment="Right" >
    <Ellipse  Height="20" Width="20" Stroke="Black" StrokeThickness="1" ></Ellipse>
    </StackPanel>
    <Ellipse x:Name="TheMiddleComponent" Grid.Row="1" Grid.Column="1"  Height="40" Width="40" Stroke="Black" StrokeThickness="1" ></Ellipse>
    <StackPanel Grid.Row="1" Grid.Column="2" x:Name="TheRightComponents" Orientation="Horizontal" HorizontalAlignment="Left" >
        <Ellipse  Height="20" Width="20" Stroke="Black" StrokeThickness="1" ></Ellipse>
        <Ellipse  Height="20" Width="20" Stroke="Black" StrokeThickness="1" ></Ellipse>
        <Ellipse  Height="20" Width="20" Stroke="Black" StrokeThickness="1" ></Ellipse>
        <Ellipse  Height="20" Width="20" Stroke="Black" StrokeThickness="1" ></Ellipse>
        <Ellipse  Height="20" Width="20" Stroke="Black" StrokeThickness="1" ></Ellipse>
    </StackPanel>
</Grid>


如果我的控件不对称(例如,我在第0列有一个小控件,在第4列有一个大控件),则此操作不起作用。此外,它还迫使我为我希望具有动态大小的控件指定固定大小。考虑下面的XAML:这个解决方案也假设我的布局是对称的;如果我的“中心”栏左边有1列,右边有100列(我不知道任何东西的固定大小,因为整个布局根据内容改变大小),“中心”的定义是在设计的中间,在某物的中间。除非集中在中间并不意味着你必须澄清你的问题,因为当我读它时,我明白你想要以某个布局项目为中心。如果你正在寻找一个布局策略,允许你把未知数量的物品放置到某个侧面,那么你正在寻找一个DokPoad,使用对象上的Dock.Left和Dock.Right属性。您的“中间”元素将停靠在左侧(例如),然后您添加的所有元素都将停靠在“Dock.Right”。然后它就变成了一个恰当地放置视图的问题。这似乎没有达到我想要的效果,因为“中间”元素没有居中。让我解释一下。我有一些未知(设计时)大小的控件,比如说7个,水平堆叠。我想要一个布局,迫使控件2始终精确地定位在窗口的中间。然后,其他控件将被放置在该居中控件的旁边(例如,1将紧靠2的左侧,3将紧靠其右侧,4将紧靠3的右侧,等等)。我试图实现的一个很好的示例是Windows Media Player的控件行,其中大播放按钮是“中心”元素