I have downloaded 3rd party autocomplete textbox and referenced it in my project. I have 3 autocomplete textboxes FirstName, LastName, ReceiptNo. When the form loads I want the firstname to be in focus. How to achieve this.
I have tried several steps like
//Eventhandler while form loads
private void Page_Loaded_1(object sender, RoutedEventArgs e)
{
FirstName.Focus();
}
or
//Eventhandler while autocomplete textbox loads
void FirstName_GotFocus(object sender, RoutedEventArgs e)
{
FirstName.Focus();
}
I also tried creating a bool isvisible property and binding it to autocomplete FirstName textbox in Xaml but that does not works. Any help will be appreciated.
My xaml code is given below
<wpf:AutoCompleteTextBox Style="{StaticResource AutoComp}"
Height="32"
Canvas.Left="33"
ToolTip="First Name"
Canvas.Top="120"
Width="205"
Padding="10,5"
TabIndex="1001"
VerticalAlignment="Top"
Loaded="FirstName_GotFocus"
Watermark=""
IconPlacement="Left"
IconVisibility="Visible"
Delay="100"
Text="{Binding FirstName, Mode=TwoWay, TargetNullValue=''}"
Provider="{Binding FirstNameSuggestions}">
<wpf:AutoCompleteTextBox.ItemTemplate>
<DataTemplate>
<Border Padding="5">
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding}"
FontWeight="Bold" />
</StackPanel>
</Border>
</DataTemplate>
</wpf:AutoCompleteTextBox.ItemTemplate>
</wpf:AutoCompleteTextBox>
<Label Style="{StaticResource Devlbl}"
Canvas.Left="250"
Content="Last Name"
Canvas.Top="90" />
<wpf:AutoCompleteTextBox Style="{StaticResource AutoComp}"
Height="32"
ToolTip="Last Name"
Canvas.Left="250"
Canvas.Top="120"
Width="205"
Padding="10,5"
TabIndex="1002"
VerticalAlignment="Top"
Watermark=""
IconPlacement="Left"
IconVisibility="Visible"
Delay="100"
Text="{Binding LastName, Mode=TwoWay, TargetNullValue=''}"
Provider="{Binding LastNameSuggestions}">
<wpf:AutoCompleteTextBox.ItemTemplate>
<DataTemplate>
<Border Padding="5">
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding}"
FontWeight="Bold" />
</StackPanel>
</Border>
</DataTemplate>
</wpf:AutoCompleteTextBox.ItemTemplate>
</wpf:AutoCompleteTextBox>
</Label>
<Label Style="{StaticResource Devlbl}"
Canvas.Left="480"
Content="Receipt No"
Canvas.Top="90" />
<!--<TextBox Canvas.Left="480"
ToolTip="Receipt No"
Canvas.Top="107"
Width="205"
MaxLength="10"
TabIndex="1003"
Style="{StaticResource CommonTextBox}"
Text="{Binding ReceiptNo,TargetNullValue=''}">
<i:Interaction.Behaviors>
<b:AllowableCharactersTextBoxBehavior RegularExpression="^[0-9]+$" MaxLength="10" />
</i:Interaction.Behaviors>
</TextBox>-->
<wpf:AutoCompleteTextBox Style="{StaticResource AutoComp}"
Height="32"
ToolTip="Receipt No"
Canvas.Left="480"
Canvas.Top="120"
Width="205"
Padding="10,5"
TabIndex="1002"
VerticalAlignment="Top"
Watermark=""
IconPlacement="Left"
IconVisibility="Visible"
Delay="100"
Text="{Binding ReceiptNo, Mode=TwoWay, TargetNullValue=''}"
e:FocusExtension.IsFocused="{Binding IsFocused, Mode=TwoWay }"
Provider="{Binding ReceiptIdSuggestions}">
<wpf:AutoCompleteTextBox.ItemTemplate>
<DataTemplate>
<Border Padding="5">
<StackPanel Orientation="Vertical" >
<TextBlock Text="{Binding}"
FontWeight="Bold">
</TextBlock>
</StackPanel>
</Border>
</DataTemplate>
</wpf:AutoCompleteTextBox.ItemTemplate>
<i:Interaction.Behaviors>
<b:AllowableCharactersTextBoxBehavior RegularExpression="^[0-9]+$" MaxLength="15" />
</i:Interaction.Behaviors>
</wpf:AutoCompleteTextBox>
</Label>
Your first attempt is very close. Try doing the following in the Page_Loaded_1 event handler
this.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
(Note - MoveFocus is a method on the Window class, it's not something you need to implement)
You should define your textboxes to have tab-indices. After the page loads, the TraversalRequest will give focus to the first tab-indexed control. If no tab indices are defined I believe it will give focus to the top of the UI hierarchy, so technically the main window would receive focus in that case.
For reference, here's an MSDN link to all of the FocusNavigationDirection options.
I changed the code as shown below and its working fine. I added a cavas as container and placed my auto complete textbox inside the canvas.
FocusNavigationDirection focusDirection = FocusNavigationDirection.Next;
// MoveFocus takes a TraveralReqest as its argument.
TraversalRequest request = new TraversalRequest(focusDirection);
UIElement elementWithFocus = Keyboard.FocusedElement as UIElement;
if (elementWithFocus != null)
{
elementWithFocus.MoveFocus(request);
}
Related
I have a GridView:
<GridViewColumn Width="{Binding ElementName=helperField, Path=ActualWidth}" >
<GridViewColumn.Header>
<StackPanel Orientation="Horizontal" >
<TextBlock Text="{x:Static lang:TextResource.name}" HorizontalAlignment="Left" Width="200" />
<TextBlock Margin="20,0,0,0" Text="{Binding SelectedWorkingDate.WorkingDate.Date, StringFormat=d}" Width="100" />
<TextBlock Margin="0,0,17,0" Text="{x:Static lang:TextResource.workingtime_time}" Width="80" />
<TextBlock Text="{x:Static lang:TextResource.desc}" Margin="15,0,0,0" Width="300" HorizontalAlignment="Right" />
</StackPanel>
</GridViewColumn.Header>
...
Now my gridViewColumn filling all the available space, but the header also is moving after f.e. maximizing the window.
What can I do to have explicit position of my StackPanel in the header.
I have also created a blank project with ListView, set the GridViewColumn width="900" and see the same behavior, so maybe there is a property I should use to change that behavior?
You can use Canvas:
<Canvas>
<TextBlock Canvas.Top="20" Canvas.Left="20" Text="{x:Static lang:TextResource.name}"
</Canvas>
How to get the content of the hyperlink button clicked in the customized list. The code of XAML is posted below.
<ListBox Height="513" HorizontalAlignment="Left" Margin="9,88,0,0" Name="listBox1" VerticalAlignment="Top" Width="436">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Height="132">
<TextBlock Text="{Binding ImageSource}" Height="73" Width="73" VerticalAlignment="Top" Margin="0,10,8,0"/>
<StackPanel Width="370">
<HyperlinkButton Content="{Binding usrname}" Click="eventhandler" Foreground="#FFC8AB14" FontSize="24" />
<TextBlock Text="{Binding msg}" TextWrapping="Wrap" FontSize="20" />
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Thanks alot in advance
Your event handler will have a source argument which is a reference to the HyperlinkButton. You can use it as follows:
public void eventhandler(object source, EventArgs e)
{
HyperlinkButton button = source as HyperlinkButton;
var foo = button.Content;
}
I am in serious trouble. I have listbox control in which i have many combo box. whenever select the value in combo box, i have to make other controls as hidden. i am using MVVM pattren. i am unable to get the child controls from ListBox control but i can get the ListBox control in viewmodel. How can i get these controls in viewmodel? Is it possible? I am using framework 4.0. i have shown the code below which write in view.
<ListBox x:Name="lstItems" MaxHeight="300" FontSize="11" Margin="12,0,20,38" ItemsSource="{Binding Source={StaticResource listedView}, Path=myItemsSource, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">
<ListBox.ItemTemplate >
<DataTemplate>
<Border BorderBrush="Blue" Margin="0,4,0,4" BorderThickness="1" CornerRadius="5">
<StackPanel Orientation="Horizontal">
<Label Content="Show rules where:" Name="lblshowrules"></Label>
<ComboBox x:Name="cboShowRuleWhere" Height="20" Width="200" ItemsSource="{Binding Source={StaticResource listedView}, Path=FilterRules}" DisplayMemberPath="RuleName" SelectedValuePath="RuleId" SelectedValue="{Binding Source={StaticResource listedView}, Path=SelectedRuleName, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" ></ComboBox>
<Grid Height="29" HorizontalAlignment="Left" Name="grid1" VerticalAlignment="Top" Width="496" Grid.Row="1" Margin="0,0,0,0">
<ComboBox Height="21" HorizontalAlignment="Left" Margin="6,4,0,0" x:Name="cboRuleCondtion" VerticalAlignment="Top" Width="212" />
<TextBox Height="20" HorizontalAlignment="Left" Margin="242,3,0,0" x:Name="txtValue" VerticalAlignment="Top" Width="245" Visibility="Hidden"/>
<ComboBox Height="21" HorizontalAlignment="Left" Margin="224,3,0,0" x:Name="cboValue" VerticalAlignment="Top" Width="205" Visibility="Hidden" />
<DatePicker Height="28" HorizontalAlignment="Left" Margin="242,-3,0,0" x:Name="dtpFromDate" VerticalAlignment="Top" Width="98" Visibility="Hidden" />
<DatePicker Height="31" HorizontalAlignment="Left" Margin="346,-3,0,0" x:Name="dtpToDate" VerticalAlignment="Top" Width="98" Visibility="Hidden"/>
</Grid>
<Button Name="cmdAddLevel" Padding="0" Margin="-1,1,0,-1" Width="75" Command ="{Binding Source={StaticResource listedView}, Path=AddLevelCommand, UpdateSourceTrigger=PropertyChanged}" BorderBrush="White" BorderThickness="1" Height="25" HorizontalContentAlignment="Center">
<StackPanel Orientation="Horizontal">
<Image Height="16" Width="16" HorizontalAlignment="Left" Margin="1,0,0,-1">
</Image>
<TextBlock Text="Add Level" FontWeight="Bold" Height="16" Width="70" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="8,2,0,-1" />
</StackPanel>
</Button>
<Label Name="lblDeleteLevel" Margin="3,0,0,0" Width="75" TabIndex="7" HorizontalAlignment="Left">
<Hyperlink >
<TextBlock Text="Delete Level" />
</Hyperlink>
</Label>
</StackPanel>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
Please help me. once again, i want to know how to get the child control from parent control... Is it possible?
Using the MVVM pattern, you should not be referencing any of the controls directly. Alternatively, you should create a boolean property on your viewmodel that decides if various controls should be visible. Then bind the Visibility property of the controls you want to hide to this property, using a converter.
See this previous Q/A for details on visibility converters: Link
Good day!
I have such a template:
<common:HierarchicalDataTemplate x:Key="my2ndPlusHierarchicalTemplate" ItemsSource="{Binding Children}">
<StackPanel Margin="0,2,5,2" Orientation="Vertical" Grid.Column="2">
<CheckBox
IsTabStop="False"
IsChecked="False"
Click="ItemCheckbox_Click"
Grid.Column="1"
/>
<TextBlock Text="{Binding Name}" FontSize="16" Foreground="#FF100101" HorizontalAlignment="Left" FontFamily="Verdana" FontWeight="Bold" />
<TextBlock Text="{Binding Description}" FontFamily="Verdana" FontSize="10" HorizontalAlignment="Left" Foreground="#FFA09A9A" FontStyle="Italic" />
<TextBox Width="100" Grid.Column="4" Height="24" LostFocus="TextBox_LostFocus" Name="tbNumber"></TextBox>
</StackPanel>
</common:HierarchicalDataTemplate>
for a Treeview
<controls:TreeView x:Name="tvServices" ItemTemplate="{StaticResource myHierarchicalTemplate}" ItemContainerStyle="{StaticResource expandedTreeViewItemStyle}" Grid.Column="1" Grid.Row="2" Grid.ColumnSpan="3" BorderBrush="#FFC1BCBC" FontFamily="Verdana" FontSize="14">
</controls:TreeView>
I want to know the Name property of each TextBox in Treeview to make validation of each textbox such as:
private void TextBox_LostFocus(object sender, RoutedEventArgs e)
{
tbNumber.ClearValidationError();
if ((!tbNumber.Text.IsZakazNumberValid()) && (tbNumber.Text != ""))
{
tbNumber.SetValidation(MyStrings.NumberError);
tbNumber.RaiseValidationError();
isValid = false;
}
else
{
isValid = true;
}
}
and I wnat to see what check boxes were checked
how can I do it?
I'm not sure I'm clear why you need to know the name.
In this case the sender parameter of the LostFocus event is the TextBox in question. Hence you could use:-
TextBox tb = (TextBox)sender;
Now use this tb variable instead of using tbNumber (which doesn't actually exist because its defined in a template).
So, let's say I have a DataTemplate:
<DataTemplate x:Key="ProjectsDataItemTemplate">
<ComboBoxItem x:Name="ProjectComboBox"
Opacity="1" HorizontalAlignment="Stretch"
Foreground="#FF80BBD2"
VerticalAlignment="Center" VerticalContentAlignment="Center"
Background="Transparent"
Style="{DynamicResource ComboBoxItemStyle1}">
<StackPanel>
<Label Content="{Binding Name}" Height="32" VerticalContentAlignment="Top"
FontWeight="Bold" Foreground="#FFFEF9F9" AllowDrop="True" />
<TextBlock Text="{Binding Description}"
Foreground="#FF80BBD2"
Padding="5,0,0,10"
FontStyle="Italic" />
</StackPanel>
</ComboBoxItem>
</DataTemplate>
In this case, the Label and the TextBlock both overlap the clickable area for the ComboBoxItem. How do I ignore and/or pass a click through to the ComboBoxItem when I click one of its child controls?
Just set the IsHitTestVisible property to false for those elements:
<DataTemplate x:Key="ProjectsDataItemTemplate">
<ComboBoxItem x:Name="ProjectComboBox"
Opacity="1"
HorizontalAlignment="Stretch"
Foreground="#FF80BBD2"
VerticalAlignment="Center"
VerticalContentAlignment="Center"
Background="Transparent"
Style="{DynamicResource ComboBoxItemStyle1}">
<StackPanel>
<Label IsHitTestVisible="False"
Content="{Binding Name}"
Height="32"
VerticalContentAlignment="Top"
FontWeight="Bold"
Foreground="#FFFEF9F9"
AllowDrop="True" />
<TextBlock IsHitTestVisible="False"
Text="{Binding Description}"
Foreground="#FF80BBD2"
Padding="5,0,0,10"
FontStyle="Italic" />
</StackPanel>
</ComboBoxItem>
</DataTemplate>