I've researched all this morning how to modify the behavior of a RadGridView (in WPF) for moving focus from one control to the next, just like the Tab key does... and like many other examples I've seen, I want to do the same thing: just make the Enter (or Return) key mimic the Tab key. But my code is not working.
If I have the following code in XAML:
<telerik:GridViewDataColumn Name="MyColumn" CellStyleSelector="{DynamicResource MyColorStyle}" Width="64">
<telerik:GridViewDataColumn.Header>
<TextBlock Text="My Column" TextAlignment="Center" VerticalAlignment="Center" TextWrapping="Wrap" />
</telerik:GridViewDataColumn.Header>
<telerik:GridViewDataColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding MyNumber}" TextAlignment="Right"></TextBlock>
</DataTemplate>
</telerik:GridViewDataColumn.CellTemplate>
<telerik:GridViewColumn.CellEditTemplate>
<DataTemplate>
<TextBox Name="MyNumberTextBox" Text="{Binding MyNumber, Mode=TwoWay}" IsTabStop="True" TabIndex="27" PreviewKeyDown="MyNumberTextBox_PreviewKeyDown" MaxLength="10" HorizontalAlignment="Stretch" VerticalAlignment="Center" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" />
</DataTemplate>
</telerik:GridViewColumn.CellEditTemplate>
</telerik:GridViewDataColumn>
And I have this code in the code-behind:
private void MyNumberTextBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
if ((e.Key == Key.Enter) || (e.Key == Key.Return))
{
e.Handled = true;
(sender as TextBox).MoveFocus(new TraversalRequest(System.Windows.Input.FocusNavigationDirection.Next));
}
}
The focus does not advance to the next column immediately to the right on the RadGridView row; it instead advances back to the top of the screen (to a pulldown menu control). I have TabStops explicitly set for every textbox in my column(s).
Could use some help here to determine what I am not doing properly, or what I may be missing. Thanks in advance for your help.
Related
Need to get the cell edited with the button click where the button is bound to the cell. I'm trying to get, but still its returning first row.
XAML:
<telerik:GridViewDataColumn Header="ABC" DataMemberBinding="{Binding ABC, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" >
<telerik:GridViewDataColumn.CellTemplate>
<DataTemplate>
<TextBlock Visibility="{Binding ABC}">
<Hyperlink NavigateUri="{Binding ABC}" RequestNavigate="Hyperlink_RequestNavigate">
<TextBlock Text="Link" />
</Hyperlink>
<telerik:RadRibbonButton Content="Edit" Grid.Column="1" VerticalAlignment="Center" Size="Small" Width="25"
SmallImage="..\Images\Edit_16.png" LargeImage="..\Images\Edit_32.png" Click="RadButtons_Click"></telerik:RadRibbonButton>
</TextBlock>
</DataTemplate>
</telerik:GridViewDataColumn.CellTemplate>
</telerik:GridViewDataColumn>
XAML.cs:
private void RadButtons_Click(object sender, RoutedEventArgs e)
{
this.grdgetval.CurrentCellInfo // This always returns the first row
this.grdgetval.BeginEdit();
}
Where i'm wrong? and what needs to be added?
I don't get exactly what you want but you could try one of the following:
this.radGridView1.CurrentCell.ColumnInfo.Name
this.radGridView1.SelectedRows[0].Cells["Picture Name"].Value
this.radGridView1.CurrentRow.Cells[0].Value
I hope this is of some help to you.
If you want to send the current cell in Edit Mode programmatically, you have to use following code -
this.grdgetval.CurrentCell.BeginEdit()
If you want to edit a cell on a button click (I assume that is your case), you have to set the desired cell as CurrentCell first, as shown below -
this.grdgetval.CurrentCellInfo = cellToEdit; // cellToEdit-the cell to be edited
this.grdgetval.BeginEdit();
Hope this helps!
I want to set the focus automatically when "NumberEnterTextBox" lost the focus.
See my XAML code here:
<StackPanel Grid.Row="0" Orientation="Horizontal" HorizontalAlignment="Left">
<TextBlock VerticalAlignment="Center" Text="Nummer eingeben: "/>
<TextBox x:Name="NumberEnterTextBox" VerticalAlignment="Center" Width="200" HorizontalAlignment="Right" KeyUp="NumberEnterTextBox_KeyUp" TextChanging="NumberEnterTextBox_OnTextChanging"/>
</StackPanel>
<Viewbox x:Name="Viewbox" Grid.Row="1" HorizontalAlignment="Left">
<Image Source="{Binding GetImageSource, Converter={StaticResource Converter}}" Stretch="Uniform"/>
</Viewbox>
Well the thing is, I tried several variations of the following snippets in my codebehind that works fine with a normal WPF application:
NumberEnterTextBox.Focus(FocusState.Pointer);
NumberEnterTextBox.Select(0, 0);
So my question is simple: How can I set the focus to the NumberEnterTextBox from my codebehind?
Well I have a solution, but I have no idea why this works in my case only within a LostFocus event:
private void NumberEnterTextBox_OnLostFocus(object sender, RoutedEventArgs e)
{
NumberEnterTextBox.Focus(FocusState.Programmatic);
}
But thanks guys for your help!
I have an ObservableCollection<Tag> Tags where the Tag class contains only a string Content property. I've created a DataTemplate that displays all tags and shows small buttons to delete and add new tags.
<DataTemplate>
<Border BorderThickness="1" BorderBrush="#676B6E" Margin="3">
<StackPanel Orientation="Horizontal">
<TextBox Text="{Binding Content, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Tag="{Binding}" Padding="0" Margin="2,0"/>
<Button Style="{StaticResource RibbonButton}" Click="ButtonRemoveTagClick" Tag="{Binding}" Padding="0">
<Image Height="12" Width="12" Source="/My Application;component/Resources/cross.png" />
</Button>
</StackPanel>
</Border>
</DataTemplate>
When I add a new Tag to the collection I want the autocreated textbox to automatically select all the text inside and grab focus.
Is there an appropriate event I can handle on the textbox itself, or is there a better way to handle this?
I tried with WpfExtendedToolkit.AutoSelectTextBox but does not work the way I want.
In this case the Loaded event of the TextBox did the trick.
private void TextBox_Loaded(object sender, RoutedEventArgs e)
{
TextBox tb = sender as TextBox;
tb.Focus();
tb.SelectAll();
}
Thanks to Clemens for the quick response.
I want to open popup by clicking on button in gridView element, inside popup I have three options by clicking on that option I want to navigate to another page with the id of the element
i want to open popup on click on button inside the gridView element my code looks like :-
<GridView x:Name="Test" Grid.Row="1" Margin="20,20,20,20" >
<GridView.ItemTemplate>
<DataTemplate>
<Grid HorizontalAlignment="Left" Width="250" Height="250">
<Button Background="Red" HorizontalAlignment="Left" VerticalAlignment="Top" Height="100" Content="OpenPopup" Click="Button_PointerPressed"></Button>
<Popup x:Name="Mypopup">
<TextBlock Text="hi"/>
</Popup>
<StackPanel VerticalAlignment="Bottom" Background="{ThemeResource ListViewItemOverlayBackgroundThemeBrush}">
<TextBlock Text="{Binding Title}" Foreground="{ThemeResource ListViewItemOverlayForegroundThemeBrush}" Style="{StaticResource TitleTextBlockStyle}" Height="60" Margin="15,0,15,0"/>
<TextBlock Text="{Binding Subtitle}" Foreground="{ThemeResource ListViewItemOverlaySecondaryForegroundThemeBrush}" Style="{StaticResource CaptionTextBlockStyle}" TextWrapping="NoWrap" Margin="15,0,15,10"/>
</StackPanel>
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
on click event
private void Button_PointerPressed(object sender, RoutedEventArgs e)
{
Mypopup.isopen = true;
}
Error:-The name 'Mypopup' does not exist in the current context
I am a newbie.. so help me
All you have to do is on the Onclick event of your gridview's button do the following code :
popup.isopen = true
Then in your popup if you want to navigate to another page all you have to do is use Navigate on the proper event
Frame.Navigate(typeof(YourPage),YourID);
EDIT : If the buttons in your gridview are added through code and you want to do the event on those then make sure that they have different names such as : GrdButton1/GrdButton2 and then give them an event dynamicly
If they are added in the XAML then simply add it an event there
<Button x:Name="ButtonTest"
Click="ButtonTest_Click"/>
If none of this answers your question please add more details to it
I have two stack panel, containing each, one datagrid.
I set the itemsSource of my datagrid when the source are loaded.
My panels are collapsed in the beginning, and then, must appears when I checked one RadioButton.
Here is the xaml of stackpanel :
<StackPanel Name="spListeList"
Grid.Row="0"
Grid.Column="2"
Orientation="Horizontal"
Visibility="Collapsed">
<sdk:DataGrid Name="dgListList"
VerticalAlignment="Top"
HorizontalAlignment="Left"
AutoGenerateColumns="False"
AlternatingRowBackground="Orange"
IsReadOnly="True">
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn Binding="{Binding IdList, Mode=TwoWay}"
Visibility="Collapsed"
Width="10"/>
<sdk:DataGridTextColumn Header=" "
Width="85"
Binding="{Binding LibList, Mode=TwoWay}"/>
</sdk:DataGrid.Columns>
</sdk:DataGrid>
</StackPanel>
<StackPanel Name="spListeJury"
Grid.Row="0"
Grid.Column="3"
Orientation="Horizontal"
Visibility="Collapsed">
<sdk:DataGrid Name="dgListeJury"
VerticalAlignment="Top"
HorizontalAlignment="Left"
AutoGenerateColumns="False"
AlternatingRowBackground="Orange"
IsReadOnly="True">
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn Binding="{Binding IdJury, Mode=TwoWay}"
Visibility="Collapsed"
Width="10"/>
<sdk:DataGridTextColumn Header=" "
Width="85"
Binding="{Binding LibJury, Mode=TwoWay}" />
</sdk:DataGrid.Columns>
</sdk:DataGrid>
</StackPanel>
I got 10 radioButton, and 2 handler(similar except for names) for them.
void rbListeList_Checked(object sender, RoutedEventArgs e)
{
try
{
if (templateColumnListeList == null)
{
templateColumnListeList = new DataGridTemplateColumn();
}
dgListeList.Columns.Remove(templateColumnListeList);
templateColumnListeList.Header = "Select";
templateColumnListeList.CellTemplate = (DataTemplate)Resources["ItemTemplateSelect"];
dgListeList.Columns.Add(templateColumnListeList);
templateColumnListeList.DisplayIndex = 0;
spListeList.Visibility = Visibility.Visible;
spListeJury.Visibility = Visibility.Collapsed;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
When I comment the lines changing the visibility, nothing goes wrong. But when it's not, I got a ArgumentOutOfBound Exception
And when the two panels are visible on the beginning, they appears well, without bug. And the template colums appears too when I click on a radioButton and goes into the handler...
I tried another one :
when i star with both visible, application running well, and panels appears and desappears the way I want... So it's seems, there is a problem when I star with both collapsed.
Can't I change visibility on panels, containing Source when application is running?
Thank you.
When you set the Visibility="Collapsed" in XAML, that control is not Instantiated when the Window Loads. Try Visibility="Hidden" if using WPF. Silverlight Doesn't support Hidden visibility. or try setting the Collapsed property in Loaded event of the Window.
I would say there is an issue with the column code above but it is hard to tell without more code. I normally bind the visibility property of the control to a property on the datacontext for the page and set that value when needed. If you post a little more code I could be of more help.