silverlight datagrid multicontrol TemplateColumn TabIndex issue - datagrid

In SL4 DataGrid I have the following multicontrol column:
<sdk:DataGridTemplateColumn Header="Address Line1
Address Line 2" MinWidth="200">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=Address1}"/>
<TextBlock Text="{Binding Path=Address2}"/>
</StackPanel>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
<sdk:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<StackPanel>
<TextBox Background="Transparent" BorderThickness="0"
TabIndex="0"
Text="{Binding Path=Address1, Mode=TwoWay}"/>
<TextBox Background="Transparent" BorderThickness="0"
TabIndex="1"
Text="{Binding Path=Address2, Mode=TwoWay}"/>
</StackPanel>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellEditingTemplate>
</sdk:DataGridTemplateColumn>
In edit mode pressing Tab key while at address1 moves focus to next DataGrid column but not to address2 textbox. If I delete CellTemplate and CellEditingTemplate to be CellTemplate instead, then TabIndex works as expected, however, current column stay the same, so if datagrid has many columns, some of which are hidden, then auto scrolling doesn't occur. What should I do to solve this problem?

A little bit late, but i found a workaround for this problem.
Just add a KeyDown EventHandler to your CustomControl:
private void address1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key.Equals(Key.Tab) && address2.IsEnabled)
{
address2.Focus();
e.Handled = true;
}
}

Probably, having multiple controls inside datagrid cell is bad idea. If multiple controls needs to be inside cell, then better way seems to be to create custom composite control and place it inside cell.

Related

How to hide keyboard of entry inside the listview in xamarin.forms.ios

How to hide keyboard of an entry.I have an entry inside of a listview. When I tap on the entry the keyboard is showing, but the keyboard is not hiding when I tap outside. The listview row is adding dynamically so in first case only one row is in it.Android is working perfectly. But in iOS this issue is there.
Please help me
<ListView HorizontalOptions="FillAndExpand" ItemsSource="_list"
VerticalOptions="FillAndExpand"
AbsoluteLayout.LayoutBounds="0,0,1,1"
AbsoluteLayout.LayoutFlags="All">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation=Horizontal>
<Label Text="Name"/>
<Entry PlaceHolder="Name" />
<Label Text="+" FontSize="50">
<Label .GestureRecognizers>
<TapGestureRecognizer Tapped="AddMore_Tapped"
CommandParameter="{Binding Id}"/>
</Label .GestureRecognizers>
</Label>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Xaml.CS
private async void AddMore_Tapped(object sender, EventArgs e)
{
_list.Add("1");
}
here is my code,
first time only one row is there after clicking the "+" icon one row will be added to listview. So in the case of listview with one row, when I tap on that entry the keyboard is showing, but when I tap outside it's not hiding.It's hiding only when I tap on the first row.
You could update your Xamarin.forms version to the last.
In ios, the keyboard would hide when you click on the other place of listview except the “+” label.
Or you could use custom renderer to make the done button, to hide the keyboard manually.
CustomEntry:cs
public class CustomEntry : Entry
{
}
CustomEntryRenderer.cs
[assembly: ExportRenderer(typeof(CustomEntry), typeof(CustomEntryRenderer))]
namespace HidekeyboardOfEntryDemo.iOS
{
public class CustomEntryRenderer : EntryRenderer
{
private UITextField textField;
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
textField = (UITextField)this.Control;
textField.ReturnKeyType = UIReturnKeyType.Done;
}
}
}
Usage
<hidekeyboardofentrydemo:CustomEntry Placeholder="Name" />
You could download HidekeyboardOfEntryDemo project from GitHub for reference.
https://github.com/WendyZang/Test.git

WP7 Databinding to Listbox from code with DataTemplate

Here is the XAML i use to display the Listbox:
<ListBox x:Name="groupedList" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
<ListBox.ItemTemplate>
<DataTemplate x:Name="myTemplate">
<StackPanel Orientation="Vertical">
<TextBlock Grid.Row="0" FontWeight="Bold" x:Name="templateHeader" />
<TextBlock Grid.Row="1" x:Name="templateCaption"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
The problem is that the Databinding has to be created in code since there is different data each time. (In fact the data comes by using an indexer[] with a dynamically selected field in a sql). My question now is how to apply the Databinding to the Listbox and the DataTemplate from code. As far as i can see there is no way access the items defined inside of a DataTemplate.... Can anyone help ?
This is my attempt so far:
Binding binding = new Binding("["+CaptionField+"]");
binding.Source = this.controller.table.Rows;
binding.Mode = BindingMode.TwoWay;
BindingOperations.SetBinding(this.groupedList.Items, ItemsControl.ItemsSourceProperty, binding);
this.groupedList.SetBinding(null, binding);
I was about to give you a simple example but I think there are plenty of better example, so please take a look at these site to see if it help:
WP7 - listbox Binding
listbox-data-binding
Please comment if anything wasn't clear.

How to identify what itemSelected during button click inside ListBox ItemTemplate

I have an ItemTemplate that contains a simple button. When I click this button I need a way to identify the row clicked to pull out the item bound to the listbox.
XAML
<ListBox Name="DemoBox" SelectionChanged="listBox_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Height="150">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="400"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid Height="120" Grid.Column="0">
<Grid.RowDefinitions>
<RowDefinition Height="60"/>
</Grid.RowDefinitions>
<TextBlock
Grid.Row="0"
Text="{Binding SomeObjProperty}"/>
</Grid>
<Grid Height="120" Grid.Column="1" Margin="0,-12,0,0">
<Button Click="ShowStuffOnMap_Click">
<Button.Background>
<ImageBrush ImageSource="images/arrow.png"/>
</Button.Background>
</Button>
</Grid>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
click handler
private void ShowStuffOnMap_Click(object sender, RoutedEventArgs e)
{
//sender is the button so ...
}
Thank you in advance
Why do you have Button with a click event inside a ListBox with a SelectionChanged event? This makes up for some scary UX, if they have different actions!
The normal approach is to have a databound ListBox, and then use the SelectionChanged event to read out the selected item.
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var listBox = sender as ListBox;
var selectedItem = listBox.SelectedItem as MyDataBoundType;
if (selectedItem != null)
{
// do stuff
}
// if you use the ListBox for navigation, set the SelectedIndex to -1
// listBox.SelectedIndex = -1;
}
But if you really really want to do it, you need to use the Tag property.
<Button Click="ShowStuffOnMap_Click" Tag="{Binding}">
And then in your event handler:
private void ShowStuffOnMap_Click(object sender, RoutedEventArgs e)
{
var button = sender as Button;
var selectedItem = button.Tag as MyDataBoundType;
}
But I still think your approach here is wrong, and stands for bad user experience, as the normal approach to a list is that the entire line in the list, is one-selection only.
The best way to do this is to have some unique identifier in the template itself. For example, you have a TextBlock - give it a unique name, and when the button is pressed, search the secondary grid for the TextBlock with the identifier and read its Text property.

Getting value of "System.Windows.Controls.TextBlock" when binding to Silverlight ComboBox

I'm attempting to use a Silverlight ComboBox with a static list of elements in a very simple binding scenario. The problem is the selected item is not returning me the Text of the TextBlock within the ComboBox and is instead returning "System.Windows.Controls.TextBlock".
My XAML is:
<ComboBox SelectedValue="{Binding Country, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True}">
<TextBlock FontSize="11" Text="Afghanistan" />
<TextBlock FontSize="11" Text="Albania" />
<TextBlock FontSize="11" Text="Algeria" />
</ComboBox>
In my C# file I'm binding to the ComboBox using:
Customer customer = new Customer() { Country = "Albania" };
DataContext = customer;
The binding does not result in Albania as the selected country and updating the ComboBox choice results in the Country being set to "System.Windows.Controls.TextBlock". I've tried fiddling around with DisplayMemberPath and SelectedValuePath but haven't found the answer. I suspect it's something really simple I'm missing.
Changing the ComboBox items to strings works. I'm not sure how you'd go about handling the case where TextBlocks were required for formatting...
<ComboBox SelectedValue="{Binding Country, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True}">
<System:String>Afghanistan</System:String>
<System:String>Albania</System:String>
<System:String>Algeria</System:String>
</ComboBox>

how to capture the "Menu Item 1" text in the dropdown button and assign to textbox

I am using a code snippet from codeproject of dropdown button.
< m:SplitButton Content="TWB" Name="btnSearch"
Grid.Row="0" Grid.Column="0"
Style="{StaticResource aeroNormalColorSplitButtonStyle}"
Click="btnSearch_Click"
Width="60" Height="30"
VerticalAlignment="Center"
HorizontalAlignment="Left"
Mode="{Binding ElementName=modeSelector, Path=SelectedItem}"
Placement="{Binding ElementName=placementSelector, Path=SelectedItem}" MouseLeftButtonDown="btnSearch_MouseLeftButtonDown"
MenuItem Header="TWB"/>
MenuItem Header="PWB">
/MenuItem>
</m:SplitButton>
so, beside this splitdown button, I have a textbox(which is basically a search box). So, as the above code snippet shows the 2 menu items as "TWB" and "PWB", I have to fill the textbox when TWB is selected from the dropdown button and also the same text should be displayed on the button too (TWB).
if i click PWB from the dropdownbutton, I should get the "PWB" name on the button and the same PWB name should be displayed in the textbox too.
Please help me.
Thank You,
Ramm
I tried by adding ListBox to the SplitButton, Now I am able to see the text in the textbox.
Modified the above Xaml code to
<m:SplitButton Content="TWB" Name="btnSearch"
Grid.Row="0" Grid.Column="0"
Style="{StaticResource aeroNormalColorSplitButtonStyle}"
Click="btnSearch_Click"
Width="60" Height="30"
VerticalAlignment="Center"
HorizontalAlignment="Left"
Mode="{Binding ElementName=modeSelector, Path=SelectedItem}"
Placement="{Binding ElementName=placementSelector, Path=SelectedItem}">
<ListBox x:Name="TransitionKind" SelectionChanged="TransitionKind_SelectionChanged">
<ListBoxItem Content="TWB"/>
<ListBoxItem Content="PWB"/>
</ListBox>
</m:SplitButton>
in the code behind,
btnSearch.Click += new RoutedEventHandler(btnSearch_Click);
TransitionKind.SelectionChanged += new System.Windows.Controls.SelectionChangedEventHandler(TransitionKind_SelectionChanged);
private void TransitionKind_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
btnSearch.Content = ((ListBoxItem)TransitionKind.SelectedItem).Content;
txtBxSearch.Text = "Search " + (string) btnSearch.Content;
}
Now, I am able to see the text in the textbox changing to the click of dropdown button.
Thank you,
Ramm

Resources