动态添加/删除行和在silverlight中控制网格

时间:2012-08-22 11:01:04

标签: silverlight

我正在尝试创建一个规则屏幕,用户可以在其中指定不同的规则,保存它们。稍后申请数据。用户还可以对AND OR条件的规则进行分组。我的疑问是,这是否是正确的方法。当用户点击删除该行应删除时,我不知道该怎么做?

以下是我的XAML:

<Grid x:Name="LayoutRoot" Background="White" Width="600" Height="200" ShowGridLines="True">
    <Grid.RowDefinitions><RowDefinition Height="30" /></Grid.RowDefinitions>
    <Grid.ColumnDefinitions><ColumnDefinition Width="135"/><ColumnDefinition Width="25"/><ColumnDefinition Width="135"/><ColumnDefinition Width="135"/>
        <ColumnDefinition Width="135"/><ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <telerik:RadComboBox Name="Header" Grid.Row="0" Width="100" HorizontalAlignment="Left">
            <telerik:RadComboBoxItem Content="And" IsSelected="True"></telerik:RadComboBoxItem>
            <telerik:RadComboBoxItem Content="Or"></telerik:RadComboBoxItem>
        </telerik:RadComboBox>
    <telerik:RadButton Content="+" Height="31" HorizontalAlignment="Left" Margin="106,0,0,0" Name="btnAdd" VerticalAlignment="Top" Width="26" Click="btnAdd_Click" />
</Grid>

背后的代码是:

   private void btnAdd_Click(object sender, RoutedEventArgs e)
    {
        var row = new RowDefinition();

        row.Height = new GridLength(30);
        LayoutRoot.RowDefinitions.Add(row);

        var columnCombo = new Telerik.Windows.Controls.RadComboBox() { Width = 100, Height = 30, HorizontalAlignment = HorizontalAlignment.Left };
        columnCombo.Items.Add("Column1");
        columnCombo.Items.Add("Column2");
        columnCombo.Items.Add("Column3");

        var operatorCombo = new Telerik.Windows.Controls.RadComboBox() { Width = 100, Height = 30, HorizontalAlignment = HorizontalAlignment.Left};
        operatorCombo.Items.Add("contains");
        operatorCombo.Items.Add("startsWith");
        operatorCombo.Items.Add("endsWith");

        var btnRemove = new Telerik.Windows.Controls.RadButton() { Width = 20, Height = 30, Content = "X" };
        btnRemove.Click += new RoutedEventHandler(btnRemove_Click);

        var btnGroup = new Telerik.Windows.Controls.RadButton() { Content = "Group", Width = 50, Height = 30 };
        btnGroup.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
        btnGroup.Click += new RoutedEventHandler(btnGroup_Click);

        var calendar = new Telerik.Windows.Controls.RadDatePicker();

        LayoutRoot.Children.Add(btnRemove);
        LayoutRoot.Children.Add(btnGroup);
        LayoutRoot.Children.Add(columnCombo);
        LayoutRoot.Children.Add(operatorCombo);
        LayoutRoot.Children.Add(calendar);

        int newrow = LayoutRoot.RowDefinitions.Count-1;

        Grid.SetRow(btnRemove, newrow);
        Grid.SetColumn(btnRemove, 1);
        Grid.SetRow(columnCombo, newrow);
        Grid.SetColumn(columnCombo, 2);
        Grid.SetRow(operatorCombo, newrow);
        Grid.SetColumn(operatorCombo, 3);
        Grid.SetRow(calendar, newrow);
        Grid.SetColumn(calendar, 4);
        Grid.SetRow(btnGroup, newrow);
        Grid.SetColumn(btnGroup, 5);

    }

    public void btnGroup_Click(object sender, RoutedEventArgs e)
    {
        throw new NotImplementedException();
    }

    public void btnRemove_Click(object sender, RoutedEventArgs e)
    {

        //TODO: Here I want to find out from which Gridrow user has clicked & remove that grid row & controls inside it.
    }

1 个答案:

答案 0 :(得分:1)

您可以使用每个按钮的Tag属性来获取要删除的行。

http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.tag(v=vs.95).aspx