This is a migrated thread and some comments may be shown as answers.

[Solved] NewRowStyle with button does not add row

6 Answers 255 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Scott Waye
Top achievements
Rank 2
Iron
Iron
Veteran
Scott Waye asked on 23 Aug 2011, 11:07 PM
Hi,

I'm trying to change the text for the new row using the NewRowStyle, but my button does nothing:

        <telerik:RadGridView ItemsSource="{Binding Data}" ShowInsertRow="True" x:Name="grd1" >
            <telerik:RadGridView.NewRowStyle>
                <Style TargetType="telerik:GridViewNewRow">
                    <Setter Property="Template">
                        <Setter.Value>
                        <ControlTemplate>
                                <telerik:RadButton Content="hello" Command="telerik:RadGridViewCommands.BeginInsert"
                                                   CommandTarget="{Binding ElementName=grd1}"/>
                        </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </telerik:RadGridView.NewRowStyle>
            <telerik:RadGridView.Columns>
                <telerik:GridViewDataColumn DataMemberBinding="{Binding A}" />
                <telerik:GridViewDataColumn DataMemberBinding="{Binding B}" />
            </telerik:RadGridView.Columns>
        </telerik:RadGridView>

I suppose its something trivial, but I can't see it.  The grid is bound to an ObservableCollection and without the style rows can be added ok.

Thanks,

Scott

6 Answers, 1 is accepted

Sort by
0
Vanya Pavlova
Telerik team
answered on 24 Aug 2011, 07:52 AM
Hi Scott Waye,

 
Since you have predefined the template in such way the behavior you get is the expected one. 
Regarding the template structure of GridViewNewRow you expect to enter data, however you have a single button and the DataCellsPresenter, which is responsible for displaying GridViewCells is missing. In these situations it would be much more appropriate to predefine the default template of GridViewNewRow and remove the elements from there rather than to define it in such way:


<telerik:GridLineWidthToThicknessConverter x:Key="GridLineWidthToThicknessConverter"/>
    <telerik:BooleanToOpacityConverter x:Key="BooleanToOpacityConverter"/>
    <telerik:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
    <SolidColorBrush x:Key="ControlInnerBorder" Color="White"/>
    <LinearGradientBrush x:Key="GridView_NewRowBackground" EndPoint="0.5,1" StartPoint="0.5,0">
        <GradientStop Color="#FF888888"/>
        <GradientStop Color="White" Offset="0.33"/>
        <GradientStop Color="#FFD0D0D0" Offset="0.66"/>
        <GradientStop Color="White" Offset="1"/>
    </LinearGradientBrush>
    <telerik:Office_BlackTheme x:Key="Theme"/>
    <SolidColorBrush x:Key="ControlOuterBorder" Color="#FF848484"/>
 
    <SolidColorBrush x:Key="GridView_GridLinesItemBorder" Color="#FFCBCBCB"/>
    <ControlTemplate x:Key="GridViewNewRowTemplate" TargetType="telerik:GridViewNewRow">
        <telerik:SelectiveScrollingGrid x:Name="grid">
            <telerik:SelectiveScrollingGrid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
            </telerik:SelectiveScrollingGrid.ColumnDefinitions>
            <telerik:SelectiveScrollingGrid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="Auto"/>
            </telerik:SelectiveScrollingGrid.RowDefinitions>
            <VisualStateManager.VisualStateGroups>
                <VisualStateGroup x:Name="FocusStates">
                    <VisualState x:Name="Unfocused"/>
                    <VisualState x:Name="Focused"/>
                </VisualStateGroup>
                <VisualStateGroup x:Name="SelectionStates">
                    <VisualState x:Name="Unselected"/>
                </VisualStateGroup>
                <VisualStateGroup x:Name="CommonStates">
                    <VisualState x:Name="Normal"/>
                    <VisualState x:Name="MouseOver"/>
                    <VisualState x:Name="Selected"/>
                </VisualStateGroup>
                <VisualStateGroup x:Name="ValueStates">
                    <VisualState x:Name="Valid"/>
                    <VisualState x:Name="Invalid">
                    </VisualState>
                </VisualStateGroup>
                <VisualStateGroup x:Name="EditStates">
                    <VisualState x:Name="ReadOnlyMode">
                        <Storyboard>
                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="PART_DataCellsPresenter">
                                <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="Collapsed"/>
                            </ObjectAnimationUsingKeyFrames>
                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="PART_AlwaysVisibleNewRow">
                                <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="Visible"/>
                            </ObjectAnimationUsingKeyFrames>
                        </Storyboard>
                    </VisualState>
                    <VisualState x:Name="EditMode">
                        <Storyboard>
                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="PART_DataCellsPresenter">
                                <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="Visible"/>
                            </ObjectAnimationUsingKeyFrames>
                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="PART_AlwaysVisibleNewRow">
                                <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="Collapsed"/>
                            </ObjectAnimationUsingKeyFrames>
                        </Storyboard>
                    </VisualState>
                </VisualStateGroup>
            </VisualStateManager.VisualStateGroups>
         
            <Border x:Name="PART_AlwaysVisibleNewRow" BorderBrush="{StaticResource ControlInnerBorder}" BorderThickness="1,1,1,2" Background="{StaticResource GridView_NewRowBackground}" Grid.ColumnSpan="4" Grid.Column="0" Margin="0" telerik:SelectiveScrollingGrid.SelectiveScrollingClip="True">
                <Grid>
                <telerik:RadButton Click="RadButton_Click"  telerik:LocalizationManager.ResourceKey="GridViewAlwaysVisibleNewRow" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                </Grid>
            </Border>
            <telerik:DataCellsPresenter x:Name="PART_DataCellsPresenter" Grid.Column="3" telerik:SelectiveScrollingGrid.SelectiveScrollingClip="True" telerik:StyleManager.Theme="{StaticResource Theme}" Visibility="Collapsed"/>
             
             
        </telerik:SelectiveScrollingGrid>
    </ControlTemplate>
    <SolidColorBrush x:Key="ItemBackground" Color="White"/>
    <Style  TargetType="telerik:GridViewNewRow">
        <Setter Property="Template" Value="{StaticResource GridViewNewRowTemplate}"/>
        <Setter Property="Background" Value="{StaticResource ItemBackground}"/>
        <Setter Property="BorderBrush" Value="{StaticResource GridView_GridLinesItemBorder}"/>
        <Setter Property="MinHeight" Value="25"/>
        <Setter Property="FontWeight" Value="Normal"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="HorizontalContentAlignment" Value="Left"/>
        <Setter Property="Padding" Value="5,0,3,0"/>
    </Style>
    


private void RadButton_Click(object sender, RoutedEventArgs e)
       {
           this.grid1.BeginInsert();
       }



Fell free to change it in the way you need. 


Regards,
Vanya Pavlova
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

0
Scott Waye
Top achievements
Rank 2
Iron
Iron
Veteran
answered on 24 Aug 2011, 11:11 PM
Thanks for the xaml..   But seriously, all that code just to change the text, its crazy.  Please provide a property to either style just the part where it says "Click here to add a new item" or even a simple property so the text can be changed without all this code.  I assume if I incorporate all that xaml, and you change the template in a future release I will have to start Blend (which I dont have) and re extract the xaml.  As I wont know if you have changed it or not, I will be forced to do this for every release just to make sure I get any enhancements you may have made.  Clearly this is not a viable long term approach.

--
Scott
0
Vanya Pavlova
Telerik team
answered on 25 Aug 2011, 08:42 AM
Hi Scott Waye,

 
If you need to modify only the default text "Click here to add new item" (not the appearance of GridViewNewRow) you may create your own CustomLocalizationManager and predefine the text for that particular key:

public class CustomLocalizationManager : LocalizationManager
    {
        public override string GetStringOverride(string key)
        {
            switch (key)
            {
                case "GridViewAlwaysVisibleNewRow":
                    return "MyCustomText";
            }
            return base.GetStringOverride(key);
        }
    }

Then just set this LocalizationManager within the MainPage's constructor:

public MainPage()
        {
            InitializeComponent();
  
            LocalizationManager.Manager = new CustomLocalizationManager();
        }


The other approach is to directly predefine the template of GridViewNewRow as it was proposed in my previous post.

 

All the best,
Vanya Pavlova
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

0
Scott Waye
Top achievements
Rank 2
Iron
Iron
Veteran
answered on 27 Aug 2011, 10:19 AM
Thanks,

I had seen that solution elsewhere, but unfortunately it suffers from not being able to change the text on a grid by grid basis.  I will lodge a feature request.
0
Scott Waye
Top achievements
Rank 2
Iron
Iron
Veteran
answered on 28 Aug 2011, 12:56 PM
Tried that xaml and it looks bad.  See attached file.
0
Vanya Pavlova
Telerik team
answered on 29 Aug 2011, 08:50 AM
Hi Scott Waye,

 

The behavior occured because you have removed the default indicator of GridViewNewRow.
If you sent me a snapshot of the desired final result I will prepare an example for you which demonstrates how to achieve it using the xaml approach. 



I look forward to hearing from you!



Kind regards,
Vanya Pavlova
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

Tags
GridView
Asked by
Scott Waye
Top achievements
Rank 2
Iron
Iron
Veteran
Answers by
Vanya Pavlova
Telerik team
Scott Waye
Top achievements
Rank 2
Iron
Iron
Veteran
Share this question
or