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.
Related
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;
}
}
Evening all, I've run into an issue with the SelectionChanged (TabControl) event being call before the LostFocus (TextBox) event.
This is a problem since the SelectionChanged is triggered during a tab change, that intern resets the ListView.SelectedIndex (TabControl>TabItem>ListView) to -1.
The textbox uses LostFocus to update/validate it's textbox.text which depend upon the SelectedIndex. The text in the textbox is stored/retrieved from a List<string> and because the index changed, the List happens to go out of bounds.
I've looked around, tired a few things also a "hack-y" approach which didn't really help.
<TabControl SelectionChanged="SelectionChanged_Tab"
<TabItem .../>
<TabItem .../>
</TabControl>
<Grid>
<Label .../>
<TextBox Name="Name" LostFocus="Lost_Focus" .../>
</Grid>
Code:
private void SelectionChanged_Tab(object sender, SelectionChangedEventArgs e)
{
if (e.Source is TabControl)
{
ListView1.SelectedIndex = -1;
ListView2.SelectedIndex = -1;
}
}
private void Lost_Focus(object sender, RoutedEventArgs e)
{
TextBox textbox = sender as TextBox;
int Index = ListView.SelectedIndex;
if (string.IsNullOrEmpty(textbox.Text) || textbox.Text == "0")
{
textbox.Text = "0";
}
switch (textbox.Name)
{
case "Name":
SomeList[Index].AProperty = textbox.Text;
break;
}
}
OK, so after think about the problem from a different perspective, I decided to simple make the TabControl, an Focus-able event and simple make it focus when selection changes:
<TabControl SelectionChanged="SelectionChanged_Tab" Focusable="True"
<TabItem .../>
<TabItem .../>
</TabControl>
Code:
private void SelectionChanged_Tab(object sender, SelectionChangedEventArgs e)
{
if (e.Source is TabControl)
{
ListView2.Focus();
ListView1.SelectedIndex = -1;
ListView2.SelectedIndex = -1;
}
if (ListView2.SelectedIndex == -1)
{
ListView1.Focus();
}
}
I know it's not the most elegant solution (In the process or re-factoring) but this seems to get the job done.
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.
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;
}
I have a webbrowser that uses a simple forward and backward navigation scheme, and may also refresh or stop navigation if the user so choses. All navigation is working correctly, but my issue arises when I try to toggle the 'refresh' and 'stop' buttons depending on whether the webbrowser is Navigating or has Navigated. I would like for the refresh button to be set to visible as long as no navigation is occuring, and for the stop button to be set to visible while navigation is occuring. My basic implementation is as follows but I cannot seem to get the buttons to toggle from Visible to Collapsed depending on these scenarios.
MainPage.xaml
<Button x:Name="RefreshButton" Content="" Style="{StaticResource RefreshBtn}" Grid.Column="0" Grid.Row="0" Visibility="Visible" Click="RefreshButton_Click" toolkit:TiltEffect.IsTiltEnabled="True">
<Button x:Name="StopButton" Content="" Style="{StaticResource StopBtn}" Grid.Column="0" Grid.Row="0" Visibility="Collapsed" Click="StopButton_Click" toolkit:TiltEffect.IsTiltEnabled="True"/>
MainPage.xaml.cs
public MainPage()
{
InitializeComponent();
//Change Navigation buttons if the browser is currently Navigating
if (TheBrowser._IsNavigating == false)
{
RefreshButton.Visibility = Visibility.Visible;
StopButton.Visibility = Visibility.Collapsed;
}
else
{
RefreshButton.Visibility = Visibility.Collapsed;
StopButton.Visibility = Visibility.Visible;
}
//while (TheBrowser._IsNavigating == true)
//{
// RefreshButton.Visibility = Visibility.Collapsed;
// StopButton.Visibility = Visibility.Visible;
//}
}
WebBrowser.xaml.cs
//Flag to check if the browser is navigating
public bool _IsNavigating = false;
void TheWebBrowser_Navigating(object sender,
Microsoft.Phone.Controls.NavigatingEventArgs e)
{
_IsNavigating = true;
}
void TheWebBrowser_Navigated(object sender,
System.Windows.Navigation.NavigationEventArgs e)
{
_IsNavigating = false;
}
My WebBrowser.xaml.cs is a webbrowser user control which is embedded into MainPage.xaml and named TheBrowser. When debugging, I can see the changes in the bool variable _IsNavigating between true and false as shown above but this change is not detected in MainPage.xaml.cs which is why I think the button visibility does not change. Any help with this issue would be greatly appreciated.. I have run out of ideas of how to fix this! Thanks in advance.
The code for setting the visibility is in the wrong place, right now it is only called upon page construction.
You have to call the code whenever your _isNavigating variable changes.
Do the following:
void ChangeVisibility()
{
if (TheBrowser._IsNavigating == false)
{
RefreshButton.Visibility = Visibility.Visible;
StopButton.Visibility = Visibility.Collapsed;
}
else
{
RefreshButton.Visibility = Visibility.Collapsed;
StopButton.Visibility = Visibility.Visible;
}
}
void TheWebBrowser_Navigating(object sender,
Microsoft.Phone.Controls.NavigatingEventArgs e)
{
_IsNavigating = true;
ChangeVisibility();
}
void TheWebBrowser_Navigated(object sender,
System.Windows.Navigation.NavigationEventArgs e)
{
_IsNavigating = false;
ChangeVisibility();
}