IsDefault causes focus instead of click - c#

I have a control with 3 text boxes and one Button that is set to IsDefault. When I am typing in one of the text boxes and press the Enter key I expect the IsDefault button to be clicked, instead it only focuses the button instead of clicking it. I have made sure that there are no other IsDefault buttons, I have checked that the IsDefaulted property of the Button is true when I press the enter button.
<Grid DockPanel.Dock="Top">
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Text="New:" VerticalAlignment="Center" Margin="5,0,0,0" Grid.Column="0"/>
<TextBox UndoLimit="10" Name="txtAddNew" Grid.Column="1" Height="23" Margin="10,0" VerticalAlignment="Center"/>
<TextBlock Text="Amendment:" VerticalAlignment="Center" Margin="5,0,0,0" Grid.Column="0" Grid.Row="1"/>
<TextBox UndoLimit="10" Name="txtAddAmend" Grid.Column="1" Height="23" Margin="10,0" VerticalAlignment="Center" Grid.Row="1"/>
<TextBlock Text="Unknown:" VerticalAlignment="Center" Margin="5,0,0,0" Grid.Column="0" Grid.Row="2"/>
<TextBox UndoLimit="10" Name="txtAddUnknown" Grid.Column="1" Height="23" Margin="10,0" VerticalAlignment="Center" Grid.Row="2"/>
<Idyllic:IdyllicButton DockPanel.Dock="Top" Margin="10,5" IsDefault="True" Name="btnAdd" Height="30" Grid.Row="3" Grid.ColumnSpan="2">
<StackPanel>
<Idyllic:CustomFillImage Image="{StaticResource Add}" Fill="{Binding RelativeSource={RelativeSource AncestorType=Button}, Path=Foreground}"/>
</StackPanel>
</Idyllic:IdyllicButton>
</Grid>
EDIT: The proposed question as duplication does not fix the issue, I do not want to set the focus of the button, that is my issue. When I press enter the button is focused instead of clicked. My button is already set to IsDefault.

I found the issue, the problem was that there was another User Control embedded in the control that also implements IsDefault, I suppose in that case the behavior is to focus the first IsDefault button on the page, including child User Controls.

Related

UserControl examples

I am new to the WPF.
I wanted to create a UserControl which contains two fields for example
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="189*"/>
<ColumnDefinition Width="328*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="53*"/>
<RowDefinition Height="64*"/>
<RowDefinition Height="62*"/>
<RowDefinition Height="141*"/>
</Grid.RowDefinitions>
<TextBlock HorizontalAlignment="Right" TextWrapping="Wrap" Text="Name:" VerticalAlignment="Center"/>
<TextBox HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" VerticalAlignment="Center" Width="120" Grid.Column="1" Margin="3,0,0,0"/>
<TextBlock HorizontalAlignment="Right" TextWrapping="Wrap" Text="Age:" VerticalAlignment="Center" Grid.Row="1"/>
<TextBox HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" VerticalAlignment="Center" Width="120" Grid.Row="1" Grid.Column="1" Margin="3,0,0,0"/>
<Button Content="Add Person" HorizontalAlignment="Left" VerticalAlignment="Center" Width="75" Grid.Row="2" Grid.Column="1"/>
</Grid>
I wanted to create my user control in such a way that other developers(Who uses my usercontrol) can be able to add extra fields to it.
Imagine a developer want to add "Address filed to the above control".
If any one have sample articles on this please provide me.
I think you are after ContentControl. See below article for an example.
The ContentControl can contain any type of common language runtime
object (such as a string or a DateTime object) or a UIElement object
(such as a Rectangle or a Panel). This enables you to add rich content
to controls such as Button and CheckBox.
How to Embed Arbitrary Content in a WPF Control

Windows Store App - XAML - Border filling page rather then wrapping Grid

I'm trying to put a <Border/> around a <Grid/> in a page, however the border appears to be bordering the page rather than the grid.
This is only XAML in my page element.
<Border Background="Black">
<Grid Background="{ThemeResource ControlBackgroundBrush}" x:Name="LoginCredentials" Margin="5" HorizontalAlignment="Center" VerticalAlignment="Center">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBlock TextWrapping="Wrap" Text="Username:" Style="{StaticResource SubheaderTextBlockStyle}" VerticalAlignment="Center" Grid.Row="0" HorizontalAlignment="Right" Margin="5"/>
<TextBox x:Name="UserName" TextWrapping="Wrap" HorizontalAlignment="Left" VerticalAlignment="Center" Text="" Grid.Row="0" Grid.Column="1" TextChanged="UserChange" Margin="5"/>
<Button x:Name="LoginButton" Content="Login" Click="LoginButton_Click" Grid.Row="2" HorizontalAlignment="Right" Margin="5" TabIndex="0"/>
<Button x:Name="CancelButton" Content="Cancel" Click="CancelButton_Click" Grid.Row="2" Grid.Column="1" />
</Grid>
</Border>
As a test I created a resource to fill the background of the <Grid/> with a colour and also filled the background of the <Border/> with a different colour. The <Grid/> ends up as a box in the center of the screen as intended, but the border <Border/> fills the entire screen. Can anyone tell me why this happens and how to get the <Border/> to fit around the <Grid/> as I want?
Got it!
<Border Background="Black" VerticalAlignment="Center" HorizontalAlignment="Center">
....
</Border>
As soon as i posted the question, what i was missing became clear!

How can I keep dynamic sized controls aligned?

I have a WPF application and I am looking for a way to align some Label and TextBox controls so that the TextBox controls are always inline, but also allow for the Label content to be dynamic (which should move the TextBoxes as required).
It is a little hard to explain so here are a couple of screenshots that should show my requirements...
Before:
After:
Notice how the first TextBox shifts to the right in order to make room for the longer text, while the second TextBox also shifts to keep inline with the first. (The behavior I want is similar to that of an HTML table with two rows of two cells each)
Keeping in mind I am fairly new to WPF (so I may be going down the wrong road completely), I have used a couple of StackPanels in order to cater for the dynamic sized labels. However, the problem of course is that both StackPanels do not know about each other.
Here is my current code:
<StackPanel Orientation="Horizontal">
<Label Content="Label 1" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<TextBox HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" VerticalAlignment="Top" Width="200"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Content="Label 2" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<TextBox HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" VerticalAlignment="Top" Width="200"/>
</StackPanel>
Is there anyway to get what I want by using StackPanels? If not, what other controls or methods can I use to meet my requirements?
If you want the columns to be sized the same for all controls, simply use a Grid.
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="23"/>
<RowDefinition Height="23"/>
</Grid.RowDefinitions>
<Label Grid.Row="0" Grid.Column="0" Content="Longer Label 1"/>
<TextBox Grid.Row="0" Grid.Column="1" TextWrapping="Wrap"/>
<Label Grid.Row="1" Grid.Column="0" Content="Label 2" />
<TextBox Grid.Row="1" Grid.Column="1" TextWrapping="Wrap"/>
</Grid>
With this setup, the first column will size to fit the widest label, and the second will take up the rest.
Just use a Grid:
<Grid Grid.Row="2" VerticalAlignment="Top">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
...
</Grid.RowDefinitions>
<TextBox Grid.Row="0" Grid.Column="0" Text="Name" Style="{StaticResource
LabelStyle}" />
<TextBox Grid.Row="0" Grid.Column="1" Text="{Binding Name,
UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource TextBoxStyle}" />
<TextBox Grid.Row="1" Grid.Column="0" Text="Age" Style="{StaticResource
LabelStyle}" />
<TextBox Grid.Row="1" Grid.Column="1" Text="{Binding Age,
UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource TextBoxStyle}" />
...
</Grid>

Double click event for grid view item

Is there is any way to get grid view item after double click. I have not found double click event in metro gird view. Only events like ItemClick and PointerPressed are there. Any suggestion?
<GridView Name="downloadGrid"
HorizontalAlignment="Left"
ItemsSource="{Binding Source={StaticResource CollectionItems}}"
Grid.Column="0" Grid.Row="2" PointerPressed="downloadGrid_PointerPressed"
ItemContainerStyle="{StaticResource CustomGridViewItemStyle}"
Width="Auto" SelectionChanged="downloadGrid_SelectionChanged">
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<WrapGrid Orientation="Horizontal" />
</ItemsPanelTemplate>
</GridView.ItemsPanel>
<GridView.ItemTemplate>
<DataTemplate>
<Grid Margin="6" Height="280" Width="200">
<Grid.RowDefinitions>
<RowDefinition Height="120"/>
<RowDefinition Height="35"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<Image Source="{Binding Path=Thumbnail}" HorizontalAlignment="Left" Grid.Row="0" Stretch="Fill"/>
<Image Grid.Row="0" Source="{Binding Path=Type, Converter={StaticResource Icon}}" MaxWidth="20" MinWidth="20" VerticalAlignment="Bottom" HorizontalAlignment="Left"/>
<TextBlock Text="{Binding Path=Caption}" TextWrapping="Wrap" Foreground="#FF017DD5" Grid.Row="1" HorizontalAlignment="Left" FontSize="11" Margin="3,0,0,0" Height="Auto"/>
<ProgressBar Grid.Row="2" Value="{Binding Path=PercentOfCompletion, Converter={StaticResource ProgressConverter}}" Width="75" Height="11" HorizontalAlignment="Left" VerticalAlignment="Center" FontFamily="Segoe UI" BorderThickness="1" Foreground="#FF78D200" Margin="3,0,0,0">
<ProgressBar.BorderBrush>
<SolidColorBrush Color="#FF78D200"/>
</ProgressBar.BorderBrush>
</ProgressBar>
<!--<TextBlock Grid.Row="2" Text="{Binding Path=PercentOfCompletion, Converter={StaticResource ProgressConverter}}" Margin="-15,0,15,0" VerticalAlignment="Center" HorizontalAlignment="Center" FontFamily="Segoe UI" Foreground="#333333" FontSize="12"/>-->
<TextBlock Grid.Row="2" HorizontalAlignment="Right" VerticalAlignment="Center" Text="{Binding Path=StatusInfo}" Style="{StaticResource MainPageTextStyle}" Margin="0,0,5,0"/>
<Button x:Name="playNow" Grid.Row="3" Content="Play Now" HorizontalAlignment="Left" VerticalAlignment="Center" Style="{StaticResource DownloadButtonStyle}" Click="playNow_Click" Margin="3,0,0,0" IsEnabled="{Binding IsPlay}"/>
<Button x:Name="pauseResumeBUtton" Grid.Row="3" Content="Pause" HorizontalAlignment="Right" VerticalAlignment="Center" Style="{StaticResource DownloadButtonStyle}" Click="pauseResumeBUtton_Click" Margin="0,4,3,2" IsEnabled="{Binding IsResume}"/>
<TextBlock Grid.Row="4" HorizontalAlignment="Left" VerticalAlignment="Center" Text="Size: " Style="{StaticResource MainPageTextStyle}" Margin="5,0,0,0"/>
<TextBlock Grid.Row="4" HorizontalAlignment="Right" VerticalAlignment="Center" Text="{Binding Path=OriginalFileSize, Converter={StaticResource FileSizeConverter}}" Style="{StaticResource MainPageTextStyle}" Margin="0,9,5,6"/>
<!--<TextBlock Grid.Row="5" HorizontalAlignment="Left" VerticalAlignment="Top" Text="Output: " Style="{StaticResource MainPageTextStyle}" Margin="5,0,0,0"/>
<TextBlock Grid.Row="5" HorizontalAlignment="Right" VerticalAlignment="Top" Text="iPhone / iPod" Foreground="#FF017DD5" FontSize="12" Margin="0,2,5,0"/>-->
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
The problem with using a Tapped (or DoubleTapped) event is that these events only fire when it detects a Tap gesture. "Well, yeah. That's exactly what I want", you may say.
Buuuuuut if you are using a mouse and you click on your item and the mouse moves a little bit (or a lot) before you release your finger, then that event is not recognized as a Tap. So that event will not fire. This can lead to the user not understanding why his clicks are not being recognized. This can be reproduced easily and consistently.
Potential Hacky Solution
The only way I can currently think of implementing what you would to do is by using the ItemClick event. When a GridViewItem is clicked, start a timer. The next time the ItemClick event fires, check if the same item was clicked within a reasonable amount of time (500ms? Less? I'm sure this is a quick Google away).
Of course you have to make sure you take care of stopping the timer and all that stuff.
As VasileMarianFălămaș wrote in the comments to your question you can use the double tapped event.
however you should question yourself if you want double tap events on a grid item. One of the best Windows 8 features is that app apps function the same way. Users shouldn't have to think if they should double tap or just tap in this application.
What action do you want to do when double tapping? opening the item? this is normally done by single tap.
here is an overview of when which gestures are used in Windows 8 apps:
http://msdn.microsoft.com/en-us/library/windows/apps/hh761498.aspx

ListBoxItem and Hyperlinks

I've got a few ListBoxItems that have an image and a textbox in them which highlights when clicked. What I'm having trouble figuring out is how to make whole listbox item doubleclick event fire to a hyperlink. Can someone assist me in this?
I've been looking at this but it seems that it is for the listbox as a whole rather than an item -- http://jarloo.com/code/wpf/wpf-listbox-doubleclick/.
Here is one of my listboxitems:
<ListBoxItem >
<Grid HorizontalAlignment="Stretch">
<Grid Margin="5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Border Grid.RowSpan="2"
BorderBrush="LightGray" BorderThickness="0"
Margin="0,0,5,0">
<Image Source="/IDE;component/Resources/Images/test1.ico" Height="64" Width="64" />
</Border>
<TextBlock Text="Google.com"
FontWeight="Bold"
Grid.Column="2"
Margin="0,0,0,5"/>
<TextBlock TextWrapping="Wrap" Text="To learn more information doubleclick this box to open the website."
Grid.Column="2" Grid.Row="2"/>
</Grid>
<Line X1="0" Y1="0" X2="0" Y2="0" Stretch="Uniform"
Stroke="DarkGray"
VerticalAlignment="Bottom"/>
</Grid>
</ListBoxItem>
You seem to be specifying concrete items rather than having them generated from an ItemsSource, the link you named does not apply. I don't quite understand what you mean when you refer to a Hyperlink since i cannot see any in your code.
To handle a normal double click you could assign a handler in the ListBoxItem itself:
<ListBoxItem MouseDoubleClick="ListBoxItem_DoubleClick">
...
Is that what you want?

Categories

Resources