Changing the Visibility of PanoramaItem using C# in Windows Phone 8 - c#

I am enabling the visibility of a panoramaItem to visible using C#. As a default the panoramaItem is set to Collapsed. See below:
<phone:PanoramaItem Header="select" Visibility="Collapsed" x:Name="SelectGroup">
<TextBlock Text="HI" Margin="30" FontSize="36"></TextBlock>
</phone:PanoramaItem>
Now there is a TextBlock and I have created a Tap event:
<TextBlock x:Name="SeachBloodGroup" Text="A+" FontSize="30" Tap="BloodGroup_Tap"></TextBlock>
Code:
private void BloodGroup_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
SelectGroup.Visibility = SelectGroup.Visibility == Visibility.Collapsed ? Visibility.Visible : Visibility.Collapsed;
}
But the panoramaItem is not showing.

Try this:
private void BloodGroup_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
if (SelectGroup.Visibility == System.Windows.Visibility.Collapsed)
SelectGroup.Visibility = System.Windows.Visibility.Visible;
else
SelectGroup.Visibility = System.Windows.Visibility.Collapsed;
}

Related

ListBox Visibility property doesn't work correctly

I have a problem with change dynamically Visivility property of Listbox in wpf. After clicking button, property change but I have to double-click to refresh it. So after one click, nothing change. How can I do this?
<ListBox Visibility="Collapsed" Height="Auto" Width="Auto" HorizontalAlignment="Stretch" Name="listBox" VerticalAlignment="Stretch">
</ListBox>
private void btListBoxAction_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (listBox.Visibility == Visibility.Visible) {
listBox.Visibility = Visibility.Collapsed;
}
else {
listBox.Visibility = Visibility.Visible;
}
}

How to set focus/place caret to SearchBox

I got this layout:
<RelativePanel>
<ButtonContent="" Name="btHumburger"/>
<Button Content="" Name="btBack"/>
<TextBlock Text="Home" Name="tbHeader"/>
<Button Content="" Name="btSearch"/>
<SearchBox Visibility="Collapsed" Name="asbSearch" LostFocus="asbSearch_LostFocus"/>
</RelativePanel>
And i want to display SearchBox only when i hit button btSearch, and hide it when focus is lost. In codebehind:
private void btSearch_Click(object sender, RoutedEventArgs e)
{
asbSearch.Visibility = Visibility.Visible;
asbSearch.Focus(FocusState.Programmatic);
btHumburger.Visibility = Visibility.Collapsed;
btBack.Visibility = Visibility.Collapsed;
tbHeader.Visibility = Visibility.Collapsed;
btSearch.Visibility = Visibility.Collapsed;
}
private void asbSearch_LostFocus(object sender, RoutedEventArgs e)
{
asbSearch.Visibility = Visibility.Collapsed;
btHumburger.Visibility = Visibility.Visible;
btBack.Visibility = mainFrame.CanGoBack ? Visibility.Visible : Visibility.Collapsed;
tbHeader.Visibility = Visibility.Visible;
btSearch.Visibility = Visibility.Visible;
}
But asbSearch dont get focused/placed caret after asbSearch.Focus(FocusState.Programmatic);, so LostFocus event wont fire. How to handle this?
Try calling asbSearch.UpdateLayout(); before setting the focus.
It seems that the Visibility change is not fully finished and the control can't receive focus before that.

Radio buttons in XAML

Trying to have a default value for my radio buttons, but get flagged an NullReferenceException error.
private void rbImperial_Checked(object sender, RoutedEventArgs e)
{
txtInches.Visibility = Visibility.Visible;
lblInches.Visibility = Visibility.Visible;
lblWeight.Text = "lbs";
lblHeight.Text = "Feet";
}
private void rbMetric_Checked(object sender, RoutedEventArgs e)
{
lblHeight.Text = "cm";
txtInches.Visibility = Visibility.Collapsed;
lblInches.Visibility = Visibility.Collapsed;
lblWeight.Text = "kg";
}
rbImperial is the defaulted radio button that should be checked.
XAML:
<RadioButton x:Name="rbMetric" Content="Metric" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="132,244,0,0" Checked="rbMetric_Checked" ClickMode="Press"/>
<RadioButton x:Name="rbImperial" Content="Imperial" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="132,199,0,0" Checked="rbImperial_Checked" IsChecked="True" ClickMode="Press" />
If I don't have the rbImperial Checked property to be True to make it defaulted to be selected it runs perfectly fine.
What am I missing?
My issue when I try to run it.
Try setting the default value in the constructor. Not the prettiest method, but it should work. Alternatively, you can bind it to the ViewModel; that should work as well.

How do I handle a selected item of LongListMultiSelector?

I have list of items in LongListMultiSelector - how to handle a selected item?
My LongListMultiSelector xaml:
<tkit:LongListMultiSelector Name="longlist" SelectionChanged="longlist_SelectionChanged">
<tkit:LongListMultiSelector.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Title}" FontSize="32" Tap="TextBlock_Tap"/>
</DataTemplate>
</tkit:LongListMultiSelector.ItemTemplate>
</tkit:LongListMultiSelector>
TextBlock tap event handler code:
private void TextBlock_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
var itemTapped = (sender as FrameworkElement).DataContext as Book;
}
LongListMultiSelector SelectionChanged event handler code:
private void longlist_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
I found part of solution here, however, The problem if at least one item is selected, then textblockTap event doesn't handle - longlist_SelectionChanged event handles everything. How can i fix that?
Once you are using LongListMultiSelector, the SelectionChanged event is fired when item is added or removed. If you want to perform the action regardless item is added/removed, I've managed to do it like this (for a simle string):
private void longlist_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string selectedItem = String.Empty;
if (e.AddedItems.Count > 0) selectedItem = e.AddedItems[0] as string;
else selectedItem = e.RemovedItems[0] as string;
MessageBox.Show(selectedItem); // do your work
}
It should run while items are selected separately by tapping, but this method will have problems when more items are added/removed at the same time - if you need it, then you should handle this also.
Your XAML DataTemplate.
<DataTemplate x:Key="listItemTemplate">
<StackPanel Orientation="Horizontal" Margin="4,4">
<TextBlock Tap="textblockTap" Margin="0,-7,0,0" Text="{Binding Name}" Style="{StaticResource PhoneTextLargeStyle}"/>
</StackPanel>
</DataTemplate>
In your CS page;
private void textblockTap(object sender, EventArgs e)
{
var file = (TextBlock)sender;
var ContentFile = (string)file.Text;
MessageBox.Show(ContentFile);
}
This example will show you the text of the selected item in the MessageBox.

Give focus to a component inside an expander

I have this requirement where I need to focus the first element inside the expander when the user press tab.
Currently (default behavior) the focus goes to the expander, I've tried to focus the first element of the expander by creating a focus event handler in the expander like this:
private void ExpanderGotFocus(object sender, RoutedEventArgs e)
{
var expander = (Expander) sender;
if (!expander.IsExpanded)
{
expander.IsExpanded = true;
this._someText.Focus();
}
}
Which doesn't work.
I've also tried to give the focus the the next element:
var tRequest = new TraversalRequest(FocusNavigationDirection.Next);
var keyboardFocus = Keyboard.FocusedElement as UIElement;
keyboardFocus.MoveFocus(tRequest);
But only works the second time ( when the expander has been at least opened once )
I've tried to put this in a thread and some other crazy ideas.
How can I give focus to the first element inside an expander? ( the first time the expander is closed )
I tried several ways and none of them worked, basically the problem is the TextBox is still rendering when the expander is expanding ( to early ).
So instead what I've found is to add the IsVisibleChanged event to the textbox so when the expander finished the textbox become visible and request the focus
XAML
<Expander GotFocus="ExpanderGotFocus">
<Expander.Header>
<TextBlock Text="{x:Static Client:Strings.XYZ}" />
</Expander.Header>
<Expander.Content>
<StackPanel>
<TextBox IsVisibleChanged="ControlIsVisibleChanged" Name="txtBox" />
</StackPanel>
</Expander.Content>
</Expander>
Code behind
private void ExpanderGotFocus(object sender, RoutedEventArgs e)
{
var expander = (Expander) sender;
if (!expander.IsExpanded )
{
expander.IsExpanded = true;
}
}
private void ControlIsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
Keyboard.Focus((IInputElement)sender);
}
Check with the following,
XAML code:
<StackPanel>
<Expander Header="Expander"
Name="expander"
Collapsed="OnCollapsed"
IsExpanded="True" >
<StackPanel>
<TextBox Text="Text1" Name="textBox1" />
<TextBox Text="Text2" Name="textBox2" />
<TextBox Text="Text3" Name="textBox3" />
</StackPanel>
</Expander>
<TextBox Text="Text4" Name="textBox4" />
</StackPanel>
in the code behind:
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
this.Loaded += delegate
{
textBox2.Focus();
};
}
private void OnCollapsed(object sender, RoutedEventArgs e)
{
var element = Keyboard.FocusedElement;
if (element != null)
{
//now is the ToggleButton inside the Expander get keyboard focus
MessageBox.Show(element.GetType().ToString());
}
//move focus
Keyboard.Focus(textBox4);
}
}

Categories