I'm trying to switch pages on the button click however it seems to load the "DataCollectionPage.xaml.cs but it doesn't appear on the screen.
The frame's Current source doesn't change.
I only seem to be able to change the frame in the constructor, not the click method.
How can I fix this?
MainWindow.xaml.cs
public void ButtonAction()
{
ContentFrame.NavigationService.Navigate(new Uri("Pages/DataCollectionPage.xaml",
UriKind.Relative));
ContentFrame.NavigationService.Refresh();
}
MainWindow.xaml.cs
<Frame Source="/CSA;component/Pages/Landing.xaml" Grid.Row="1" x:Name="ContentFrame"
Grid.Column="1" Grid.ColumnSpan="2" Grid.RowSpan="2" Background="#2C2F33" Opacity="0.85"
/>
Related
I have found this issue here:
WPF Navigate Pages from outside frame
But never solved, I have the same issue and same window structure:
<DockPanel>
<Frame x:Name="Menu" Source="/Menu.xaml" NavigationUiVisibility="Hidden" />
<Frame x:Name="Content" Source="/Welcome.xaml" NavigationUiVisibility="Hidden" />
</DockPanel>
As you can see, the frames contains some default pages, and Menu frame displays the menu buttons:
<Grid>
<Button x:Name="Contact" Click="Contact_Click" />
<Button x:Name="Discussion" Click="Discussion_Click" />
</Grid>
What I want to do is to change the source in Content, using the buttons in menu, I have already tried:
private void Contact_Click(object sender, RoutedEventArgs e)
{
MainWindow mainWindow = new MainWindow();
mainWindow.Content.NavigationService.Navigate("/Contact.xaml", UriKind.Relative);
}
Sideways, do you think that is better option to take the menu directly into the main window instead of having it inside a different frame?
Im trying to use the AdaptiveGridView as a main menu to navigate to other pages so I thought I'd test a click event on it, from my understanding of the sample repo that can be found below you can use a clicked event when click is enabled to do something depending on the item clicked:
https://github.com/Microsoft/UWPCommunityToolkit/tree/master/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/AdaptiveGridView
Seems straight forward but when I try trigger the click event in my own code nothing happens when a popup should appear.
C# Click Event:
private async void AGVC_ItemClick(object sender, Windows.UI.Xaml.Controls.ItemClickEventArgs e)
{
await new MessageDialog($"You clicked {(e.ClickedItem as AdaptItem).Title}", "Item Clicked").ShowAsync();
}
The grid in my XAML page:
<Controls:AdaptiveGridView Name="AGVC"
OneRowModeEnabled="False"
ItemHeight="200"
DesiredWidth="200"
SelectionMode="Single"
IsItemClickEnabled="True"
VerticalAlignment="Top"
ItemTemplate="{StaticResource MenuTemplate}" HorizontalAlignment="Left"/>
<StackPanel VerticalAlignment="Bottom" Margin="0,24,0,0" Grid.Row="1" Background="{ThemeResource SystemControlBackgroundAccentBrush}">
Item clicked was missing from the AdaptiveGridView
<Controls:AdaptiveGridView Name="AGVC"
OneRowModeEnabled="False"
ItemHeight="200"
DesiredWidth="200"
SelectionMode="Single"
IsItemClickEnabled="True"
VerticalAlignment="Top"
ItemClick="AGVC_ItemClick"
ItemTemplate="{StaticResource MenuTemplate}" HorizontalAlignment="Left" />
I'm quite new to c# wpf and have a problem.
I have used the answer from this post to duplicate a Grid control. The grid control contains a button. It looks like it is being duplicated correctly.
When the original control's button is pressed, the click event is handled which calls a method in the window's code.
When the copy of the control's button is pressed, the click event is not fired and the method is not called. This is confusing me as I want it to call that same method.
Maybe the event handling data is not being copied properly? Is there a way around this?
Both the origional grid and copied grid (containing the buttons) are children of another grid.
Edit:
This is the xaml for the origional grid which contains a button:
<Grid Name="TempTab" DockPanel.Dock="Left" HorizontalAlignment="Left" Margin="5,5,5,0">
<Rectangle Fill="Black" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Stroke ="White" Margin="0,0,-2,0">
</Rectangle>
<Grid>
<DockPanel LastChildFill="False">
<TextBlock Foreground="White" HorizontalAlignment="Left" VerticalAlignment="Bottom" Margin="3,0,3,3">Some Text</TextBlock>
<Button Width="50" BorderBrush="{x:Null}" Foreground="{x:Null}" BorderThickness="0" Margin="3,0,0,0" Click="tabdowntest">
<Button.Background>
<ImageBrush ImageSource="TopMenuBar_Close.png" Stretch="Uniform"/>
</Button.Background>
</Button>
</DockPanel>
</Grid>
</Grid>
This grid is a child of a DockPanel with name 'TabsDock'.
It is being copied with the following code:
string gridXaml = XamlWriter.Save(TempTab);
StringReader stringReader = new StringReader(gridXaml);
XmlReader xmlReader = XmlReader.Create(stringReader);
Grid newTab = (Grid)XamlReader.Load(xmlReader);
TabsDock.Children.Add(newTab);
This is the code for the 'Click' event handler which should be called when the either the origional or the copied button's are pressed. But it is only called for the origional:
private void tabdowntest(object sender, MouseButtonEventArgs e)
{
Console.WriteLine("Button Pressed");
}
The bindigs are not set, you need to set them (comment in the orig post):
To be clear, this is only half the solution (as it stood back in 08). This will cause bindings to be evaluated and the results be serialized. If you wish to preserve bindings (as the question asked) you must either add a ExpressionConverter to the Binding type at runtime (see the second part of my question for the relevant link) or see my own answer below for how to do it in 4.0.
I'm trying to programatically create a button flyout, within my XAML I have:
<Page.Resources>
<Button x:Key="LaunchFlyout" Content="LAUNCH">
<Button.Flyout>
<Flyout Placement="Top">
<Grid Width="200" Height="200">
<StackPanel>
<Rectangle Fill="Red" Width="100" Height="100" />
<Rectangle Fill="Green" Width="100" Height="100" />
</StackPanel>
</Grid>
</Flyout>
</Button.Flyout>
</Button>
</Page.Resources>
Nested within grids I have:
<Grid x:Name="launchBtn_grid" Grid.Column="1">
</Grid>
And then in my code within the Page_Loaded method I have:
bool hasContainer = localSettings.Containers.ContainsKey("appStatus");
if (!hasContainer) {
Button button = (Button)this.Resources["LaunchFlyout"];
launchBtn_grid.Children.Add(button);
}
else {
Button button = new Button();
button.Content = "LAUNCH";
button.Click += launch_btn_Click;
launchBtn_grid.Children.Add(button);
}
When I debug this, it reaches the IF statement and reaches this line launchBtn_grid.Children.Add(button); and then I get this error Element is already the child of another element.
Does anyone understand why? I have already looked and they dont already exist so I don't understand why it is giving me this error. Does anyone know what I am doing wrong?
I'm not sure in what context/use case your are doing that, but it feels weird to me to have an actual control as a Resource (not a DataTemplate, Style, etc).
If you only want to have 1 button of the 2 different template, why not switch Visibility on the 2 instead of loading controls from your code behind ?
Going forward with the idea, just add both buttons in the Grid within your XAML and switch their Visibility according to the setting you read.
There is a BooleanToVisibilityConverter within the framework to help you with this.
I'm very new in WindowsPhone development and at this moment I'm facing a problem that I couldn't find a way to fix. I'm using a LongListSelector to show some information on the device screen and also a Header and a Footer with some static information. Everything looks right, but I can't handle the Click/Tap on the Header and on the Footer (the other items are working properly).
Someone know how to do it? Is there any event that tells my .cs that someone clicked on the footer or on the header view?
Thanks!
Implement ListFooter and ListHeader in your xaml.
Example :
<phone:LongListSelector x:Name="myLongListSelector"
Background="Transparent"
IsGroupingEnabled="True"
HideEmptyGroups="True">
<phone:LongListSelector.ListFooter>
<Grid Height="70" Tap="Grid_Tap">
<TextBlock Text="Texxt" Foreground="White"
VerticalAlignment="Center"
FontWeight="Bold"
FontSize="20"/>
</Grid>
</phone:LongListSelector.ListFooter>
</phone:LongListSelector>
Now in your cs file, you have :
private void Grid_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
}