按钮宽度未设置为指定值

时间:2017-05-10 02:08:04

标签: wpf

我正在实现一个按钮网格,其中一列显示一个,其他两个折叠开始。 已将网格宽度设置为自动 将显示的按钮大小设置为BUTTONWIDTH = 50 将折叠按钮大小设置为EXTNBUTTONWIDTH = 20

我只显示第一个按钮的折叠按钮点击事件。我看到两个按钮尺寸都不符合我指定的宽度尺寸。当我在调试器中查看按钮的宽度时,宽度和最大宽度为50但实际宽度为98.但按钮的高度显示为我设置的。

我已附上代码,请查看并告诉我您如何解决问题的建议。

for (int leftCol = 0; leftCol < 3; leftCol++)
        {

            this.leftButtonGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto });
            for (int leftRow = 0; leftRow < CHARTDOMLEN; leftRow++)
            {
                this.leftButtonGrid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });

                Button btnOrderSell = new Button();
                btnOrderSell.HorizontalContentAlignment = HorizontalAlignment.Left;

                btnOrderSell.Height = BUTTONHEIGHT;
                btnOrderSell.BorderBrush = Brushes.Black;
                // To avoid double borderline
                if (leftRow > 0)
                    btnOrderSell.BorderThickness = new Thickness() { Top = 0, Bottom = 1, Left = 1, Right = 1 };

                btnOrderSell.Click += Button_Click;
                if (leftCol == 0)
                {
                    btnOrderSell.MaxWidth = BUTTONWIDTH;
                    btnOrderSell.Width = BUTTONWIDTH;
                    this.TL0[leftRow] = btnOrderSell;
                }
                else
                {
                    btnOrderSell.MaxWidth = EXTNBUTTONWIDTH;
                    btnOrderSell.Width = EXTNBUTTONWIDTH;
                    btnOrderSell.Visibility = Visibility.Collapsed;
                    if (leftCol == 1)
                    {
                        this.TL1[leftRow] = btnOrderSell;
                    }
                    else
                        this.TL2[leftRow] = btnOrderSell;                        
                }
                Grid.SetColumn(btnOrderSell, leftCol);
                Grid.SetRow(btnOrderSell, leftRow);
                this.leftButtonGrid.Children.Add(btnOrderSell);
            }
        }

XAML代码

<Grid x:Name="OrderBuyGridButtons">
<Grid.ColumnDefinitions>
    <ColumnDefinition Width="30" />
    <ColumnDefinition Width="Auto"/>
    <ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
    <RowDefinition Height="15"/>
    <RowDefinition Height="24"/>
    <RowDefinition Height="24"/>
    <RowDefinition Height="24"/>
</Grid.RowDefinitions>
<!--<Button x:Name="btn00" Grid.Column="0" Grid.Row="0" Width="30" Visibility="Collapsed" />-->
<Button x:Name="btn00" Grid.Column="0" Grid.Row="1" Width="30" Visibility="Collapsed"/>
<Button Grid.Column="0" Grid.Row="2" Width="30" Visibility="Collapsed" />
<Button Grid.Column="0" Grid.Row="3" Width="30" Visibility="Collapsed" />
<!--<Button x:Name="btn01" Grid.Column="1" Grid.Row="0" Width="30"  Visibility="Collapsed" />-->
<Button x:Name="btn01" Grid.Column="1" Grid.Row="1" Width="30" Visibility="Collapsed" />
<Button Grid.Column="1" Grid.Row="2" Width="30" Visibility="Collapsed" />
<Button Grid.Column="1" Grid.Row="3" Width="30" Visibility="Collapsed" />

<!--<Button Grid.Column="2" Grid.Row="0" Width="80" />-->
<Button Grid.Column="2" Grid.Row="1" Width="80" Background="Gainsboro" Click="Button_Click" BorderBrush="Black"/>
<Button Grid.Column="2" Grid.Row="2" Width="80" Background="Gainsboro" BorderBrush="Black"/>
<Button Grid.Column="2" Grid.Row="3" Width="80" Background="Gainsboro" BorderBrush="Black"/>

0 个答案:

没有答案