在网格wpf中自动更新按钮位置

发布于 2024-11-05 09:24:09 字数 272 浏览 2 评论 0原文

我有 3*3 网格,里面有 9 个按钮。这些按钮的可用性是在运行时确定的,因此按钮必须排列在可用空间中。

示例:

b1 b2 b3

b4 b5 b6

b7 b8 b9

如果 b5 按钮不可用,那么我必须这样做

b1 b2 b3

b4 b6 b7

b8 b9

当前在可见性更新处理程序中,我正在检查所有控件状态并更改 grid.row和 grid.column。有更好的方法吗?

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 way of doing it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

话少心凉 2024-11-12 09:24:09

根据 Bala R 的回答,您似乎正在尝试实现自己的 WrapPanel。

内置的 WrapPanel 可以自动重新排列您的控件,“从左到右,然后从上到下”或“从上到下,然后从左到右”。然后,您不再需要“观察”按钮的可见性,因为一旦一个按钮不可见(折叠),其他按钮就会立即取代后者占据的位置,然后再遵循上述模式。

这是一个快速而肮脏的示例来说明,请随意使用 WrapPanel 方向和折叠/隐藏按钮状态:

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>

代码隐藏:

    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;
            }
        }
    }

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;
            }
        }
    }
羞稚 2024-11-12 09:24:09

尝试使用 UniformGrid 并将“列”和“行”设置为 3。它将按照您刚才描述的方式自动填充网格。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文