Grid SharedSizeGroup在整个视图中不起作用

时间:2019-01-26 02:06:27

标签: wpf xaml

我有一个简单的应用程序,我希望视图中的所有按钮都具有相同的大小。此代码的问题在于,网格行0中的所有按钮的宽度相同,但是网格行2中的按钮不相同。请帮忙。

我希望网格第2行中的按钮与网格第0行具有相同的宽度。 (是否可以使用这样的Grid进行操作。请不要告诉我Uniform Grid。我知道它可以与UniformGrid一起使用,并且我也不想绑定ElementName = so,所以Path = ActualWidth)< / p>

<Grid Grid.IsSharedSizeScope="True">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid Grid.Row="0" Grid.IsSharedSizeScope="True" x:Name="InstrumentOperationsPanel" >
            <Grid.ColumnDefinitions>
                <ColumnDefinition SharedSizeGroup="A"></ColumnDefinition>
                <ColumnDefinition SharedSizeGroup="A"></ColumnDefinition>
                <ColumnDefinition SharedSizeGroup="A"></ColumnDefinition>
                <ColumnDefinition SharedSizeGroup="A"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Button x:Name="PrimeButton" Grid.Column="0" Content="ABCD"/>
            <Button x:Name="FlushButton" Grid.Column="1" Content="ABCDEFGHIJKL"/>
            <Button Content="ABCD" Grid.Column="2"/>
            <Button x:Name="PrimeFlush3TimesButton" Grid.Column="3" Content="ABCD"/>

        </Grid>
        <Separator Grid.Row="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
        </Separator>

        <Grid Grid.IsSharedSizeScope="True" x:Name="BuiltInUserActionPanelForQX200" Grid.Row="2" VerticalAlignment="Top">

            <Grid.ColumnDefinitions>
                <ColumnDefinition SharedSizeGroup="A"></ColumnDefinition>
                <ColumnDefinition SharedSizeGroup="A"></ColumnDefinition>
                <ColumnDefinition SharedSizeGroup="A"></ColumnDefinition>
                <ColumnDefinition SharedSizeGroup="A"></ColumnDefinition>
            </Grid.ColumnDefinitions>

            <Button x:Name="DRDiagnosticsButton" Grid.Column="0"
                Content="ABCD"></Button>
            <Button x:Name="QXFieldAgentButton" Grid.Column="1"
                Content="ABCD"></Button>
            <Button x:Name="QX200MTSButton" Grid.Column="2"
                Content="ABCD"></Button>
            <Button x:Name="QX200MTSDetectorButton" Grid.Column="3"
                Content="ABCD"></Button>
        </Grid>
    </Grid>

2 个答案:

答案 0 :(得分:1)

您应该只在最外面的网格上设置ISSharedSizeScope = True。

从内部网格(InstrumentOperationsPanel和BuiltInUserActionPanelForQX200)中删除Grid.IsSharedSizeScope =“ True”

答案 1 :(得分:0)

您需要进行一些更改。

a)在您的主网格中,

<Grid Grid.IsSharedSizeScope="True">

b)在InstrumentOperationsPanel的网格中,删除Grid.IsSharedSizeScope

<Grid Grid.Row="0"  x:Name="InstrumentOperationsPanel">

c)在BuiltInUserActionPanelForQX200网格中,删除Grid.IsSharedSizeScope

<Grid  x:Name="BuiltInUserActionPanelForQX200" Grid.Row="2" VerticalAlignment="Top">

您的整个Xaml看起来都不像

<Grid Grid.IsSharedSizeScope="True">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid Grid.Row="0"  x:Name="InstrumentOperationsPanel">
            <Grid.ColumnDefinitions>
                <ColumnDefinition SharedSizeGroup="A"></ColumnDefinition>
                <ColumnDefinition SharedSizeGroup="A"></ColumnDefinition>
                <ColumnDefinition SharedSizeGroup="A"></ColumnDefinition>
                <ColumnDefinition SharedSizeGroup="A"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Button x:Name="PrimeButton" Grid.Column="0" Content="ABCD"/>
            <Button x:Name="FlushButton" Grid.Column="1" Content="ABCDEFGHIJKL"/>
            <Button Content="ABCD" Grid.Column="2"/>
            <Button x:Name="PrimeFlush3TimesButton" Grid.Column="3" Content="ABCD"/>

        </Grid>
        <Separator Grid.Row="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
        </Separator>

        <Grid  x:Name="BuiltInUserActionPanelForQX200" Grid.Row="2" VerticalAlignment="Top">

            <Grid.ColumnDefinitions>
                <ColumnDefinition SharedSizeGroup="A"></ColumnDefinition>
                <ColumnDefinition SharedSizeGroup="A"></ColumnDefinition>
                <ColumnDefinition SharedSizeGroup="A"></ColumnDefinition>
                <ColumnDefinition SharedSizeGroup="A"></ColumnDefinition>
            </Grid.ColumnDefinitions>

            <Button x:Name="DRDiagnosticsButton" Grid.Column="0"
                Content="ABCD"></Button>
            <Button x:Name="QXFieldAgentButton" Grid.Column="1"
                Content="ABCD"></Button>
            <Button x:Name="QX200MTSButton" Grid.Column="2"
                Content="ABCD"></Button>
            <Button x:Name="QX200MTSDetectorButton" Grid.Column="3"
                Content="ABCD"></Button>
        </Grid>
    </Grid>

输出

enter image description here