开发者

Update button location automatically in grid wpf

I have 3*3 grid and have 9 button in it. These button availablity are determined during run time,so button has to arrange in the available space.

example:

b1 b2 b3

b4 b5 b6

b7 b8 b9

if b5 button is not available then I have to do like this

b1 b2 b3

b4 b6 b7

b8 b9

Currently in the visiblity update handler I am checking all the controls status and changing grid.row and grid.column. Is there any better wa开发者_Go百科y of doing it?


Following Bala R's answer, it seems that your are trying to implement you own WrapPanel.

There is built in WrapPanel that does such things an re-arrange automaticaly your controls either "from left to right then from top to bottom" or "from top to bottom then from left to right". Then you dot not need to "watch" your buttons' visibility anymore since as soon one is not visible (collapsed), the others immediately take the place the later occupied before following the pattern described above.

here is a quick and dirty sample to illustrate, feel free to play with WrapPanel orientation and Collapsed/Hidden button status :

XAML :

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="31*" />
        <RowDefinition Height="731*" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="284*" />
        <ColumnDefinition Width="294*" />
    </Grid.ColumnDefinitions>
    <StackPanel Orientation="Horizontal"  VerticalAlignment="Top">
        <Button x:Name="hideBtn" Content="HIDE button #" Click="hideBtn_Click"></Button>
        <TextBox x:Name="buttonNumber" Width="50"></TextBox>
        <RadioButton x:Name="radioCollapsed" Content="Collapsed" IsChecked="True"></RadioButton>
        <RadioButton x:Name="radioHidden" Content="Hidden"></RadioButton>
    </StackPanel>

    <WrapPanel x:Name="wp" Orientation="Horizontal"  Grid.Row="1" VerticalAlignment="Top" HorizontalAlignment="Left" Height="74" Width="61">
        <Button x:Name="b_1" Content="B1"></Button>
        <Button x:Name="b_2" Content="B2"></Button>
        <Button x:Name="b_3" Content="B3"></Button>
        <Button x:Name="b_4" Content="B4"></Button>
        <Button x:Name="b_5" Content="B5"></Button>
        <Button x:Name="b_6" Content="B6"></Button>
        <Button x:Name="b_7" Content="B7"></Button>
        <Button x:Name="b_8" Content="B8"></Button>
        <Button x:Name="b_9" Content="B9"></Button>
    </WrapPanel>
</Grid>

Code behind :

    private void hideBtn_Click(object sender, RoutedEventArgs e)
    {
        foreach (var child in wp.Children)
        {
            var btn = (Button)child;
            btn.Visibility = Visibility.Visible;
        }
        foreach (var child in wp.Children)
        {
            var btn = (Button)child;

            if (btn.Name.Contains(buttonNumber.Text))
            {
                if (radioCollapsed.IsChecked.Value)
                    btn.Visibility = Visibility.Collapsed;
                else
                    btn.Visibility = Visibility.Hidden;
            }
        }
    }


Try UniformGrid and set Columns and Rows to 3. It will automatically fill the grid the way you just described.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜