网格单元格中的2个元素

时间:2012-03-07 15:43:01

标签: wpf vb.net grid

我通过下拉列表和网格中单元格中的复选框动态替换标签,如下所示

Private Sub replaceUiElementInGrid(ByVal newUiElement As UIElement, ByVal uiElementToReplace As UIElement, ByVal gridToAddNewUiElement As Grid)
        If gridToAddNewUiElement.Children.Contains(uiElementToReplace) Then
            Grid.SetRow(newUiElement, Grid.GetRow(uiElementToReplace))
            Grid.SetColumn(newUiElement, Grid.GetColumn(uiElementToReplace))
            Dim pos As Integer = gridToAddNewUiElement.Children.IndexOf(uiElementToReplace)
            gridToAddNewUiElement.Children.RemoveAt(pos)
            gridToAddNewUiElement.Children.Add(newUiElement)

            Try
                Dim label As Label = DirectCast(uiElementToReplace, Label)
                If (label.Name.Contains("ACTIVITY")) Then
                    Dim checkbox As CheckBox
                    checkbox = New CheckBox()
                    checkbox.Content = "Direction"

                    checkbox.HorizontalAlignment = Windows.HorizontalAlignment.Right
                    'Dim uiCheckbox As UIElement = DirectCast(checkbox, UIElement)
                    Grid.SetRow(checkbox, Grid.GetRow(uiElementToReplace))
                    Grid.SetColumn(checkbox, Grid.GetColumn(uiElementToReplace))
                    gridToAddNewUiElement.Children.Add(checkbox)
                    AddHandler checkbox.Click, AddressOf filtrerDropDownActiviteEvent
                End If
            Catch ex As Exception

            End Try
        End If
    End Sub

问题是该复选框位于下拉列表中。我试过checkbox.HorizontalAlignment = Windows.HorizontalAlignment.Right 但是下拉列表总是占用所有单元格宽度。

我想要的只是左侧的下拉菜单和右侧的复选框,但我无法知道该怎么做

谢谢

这是最终版本

 Private Sub replaceUiElementInGrid(ByVal newUiElement As UIElement, ByVal uiElementToReplace As UIElement, ByVal gridToAddNewUiElement As Grid)
    If gridToAddNewUiElement.Children.Contains(uiElementToReplace) Then
        Dim panel As WrapPanel = New WrapPanel
        Grid.SetRow(panel, Grid.GetRow(uiElementToReplace))
        Grid.SetColumn(panel, Grid.GetColumn(uiElementToReplace))
        Dim pos As Integer = gridToAddNewUiElement.Children.IndexOf(uiElementToReplace)
        gridToAddNewUiElement.Children.RemoveAt(pos)
        panel.Children.Add(newUiElement)

        Try
            Dim label As Label = DirectCast(uiElementToReplace, Label)
            If (label.Name.Contains("ACTIVITY")) Then
                Dim checkbox As CheckBox
                checkbox = New CheckBox()
                checkbox.Content = "Direction"

                'checkbox.HorizontalAlignment = Windows.HorizontalAlignment.Right
                'Dim uiCheckbox As UIElement = DirectCast(checkbox, UIElement)
                panel.Children.Add(checkbox)
                AddHandler checkbox.Click, AddressOf filtrerDropDownActiviteEvent
            End If
        Catch ex As Exception

        End Try
        gridToAddNewUiElement.Children.Add(panel)
    End If
End Sub

1 个答案:

答案 0 :(得分:0)

在下拉列表和复选框周围换行网格或其他面板类型。格式化它,然后用网格(或其他)替换标签。

编辑:

更具体地说,现在你最终得到了这个:

<Grid>
    <ComboBox />
    <CheckBox />
</Grid>

但是应该采用这样的方式:

 <Grid>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width=".50" />
                <ColumnDefinition Width=".50" />
            </Grid.ColumnDefinitions>
            <ComboBox Grid.Column="0"  />
            <CheckBox Grid.Column="1"  />
        </Grid>
 </Grid>