In my main page I try to show a list of stuff, and on this stuff a userControl as an overlay. In fact I never see it. (In the design view, my userControl is oaky)
XAML MainPage
<Grid>
<Grid x:name="MyPage">
<!-- All this part is visible -->
//Button
//Button
//nice Pic
//Button
</Grid>
<cat:CatPagecontrol x:Name="CatTool" Visibility="Visible" HorizontalAlignment="Center" VerticalAlignment="Center">
<cat:CatPagecontrol.Transitions>
<TransitionCollection>
<PopupThemeTransition/>
</TransitionCollection>
</cat:CatPagecontrol.Transitions>
</cat:CatPagecontrol>
<!-- EDIT I remove the Grid "CatGrid" And the ZIndex -->
</Grid>
I try to switch the ZIndex, no results.
C# File
public MainView()
{
this.CatTool = new CatPagecontrol();
//this.CatTool.Visibility = Visibility.Collapsed;
}
private void showCatSelector()
{
this.CatTool.Visibility = Visibility.Visible;
}
After that I need that one of my buttons show the overlay when clicked.
If you know how to show it, I'm yours. Thanks.
edit : solution find.
Voila !
I've find my problem :
public CatPagecontrol()
{
this.InitializeComponent();
}
I just Initialized in the correct section.
Related
I have a screen with a ListView that shows a collection of comments. Also, I have a StackLayout overlapping the end of the ListView, which has an Entry and a Button to add a new comment.
I want to hide/show this StackLayout depending on the ListView scrolling direction:
If the user scrolls down -> hide the StackLayout.
If the user scrolls up -> show the StackLayout.
Anyone knows a way to accomplish that behavior?
Thanks in advance!
Xamarin.Forms ListView provides an OnItemAppearing event you can subscribe to. With this you can track your scroll direction by finding the index of the item that appeared and comparing it to the last item that appeared. Try something like this:
public partial class MainPage : ContentPage
{
public ObservableCollection<MyItemType> Items { get; set; } = new ObservableCollection<MyItemType>();
int lastItemIndex;
int currentItemIndex;
public MainPage()
{
...
listView.ItemAppearing += ListView_ItemAppearing;
}
void ListView_ItemAppearing(object sender, ItemVisibilityEventArgs e)
{
MyItemType item = e.Item as MyItemType;
currentItemIndex = Items.IndexOf(item);
if (currentItemIndex > lastItemIndex)
{
stackLayout.IsVisible = false;
}
else
{
stackLayout.IsVisible = true;
}
lastItemIndex = currentItemIndex;
}
}
EDIT: Flickering is really due to ListView being resized when the StackLayout shows and hides, so make sure that the ListView is not getting resized. Perhaps put the ListView and the StackLayout in a grid so that when you show and hide the StackLayout the ListView does not get resized, e.g.:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="35" />
</Grid.RowDefinitions>
<ListView x:Name="listView"
ItemsSource="{Binding Items}"
Grid.Row="0">
<ListView.ItemTemplate>
...
</ListView.ItemTemplate>
</ListView>
<StackLayout x:Name="stackLayout"
Grid.Row="1">
...
</StackLayout>
</Grid>
With the above, the flickering no longer occurs.
I have a simple application, a Grid with a ChromiumWebBrowser inside. When I create the ChromiumWebBrowser in XAML, it works. But when I create it from the code-behind and add it to the grid, it doesn't show up - I just get the Grid's black background.
<!--This works-->
<Grid Name="gRight" Background="Black">
<cefSharp:ChromiumWebBrowser x:Name="cefBrowser" Margin="0,20,0,0" Address="http://www.google.com" />
</Grid>
The code that doesn't work :
public Window1()
{
InitializeComponent();
bool status =CefSharp.Cef.Initialize();
ChromiumWebBrowser browse = new ChromiumWebBrowser();
browse.Load("http://www.google.com");
browse.Width = 500;
browse.Height = 500;
browse.Margin = new Thickness(0,20,0,0);
gRight.Children.Add(browse); // adding to Grid, it doesn't show up
}
Does anyone know why adding from the code-behind doesn't work in this case ?
I tried a few things about adding my own created user control element to listbox or stackpanel, but instead of gaining any success, it causes a NullReferenceException and I have no idea why ...
my user control is looking like that:
public partial class ShiftInformationItem : UserControl
{
public ShiftInformationItem()
{
InitializeComponent();
}
}
and the xaml:
<Grid>
<LabelContent="Benutzername:" />
<Label Content="01.03.2014 14:19" />
<TextBox Text="Eintrag ..." />
<Expander Header="Comments (0)">
<Grid Background="#FFE5E5E5"/>
</Expander>
</Grid>
Inside the main window I can add it in a listbox or a stackpanel without any Trouble:
<ListBox>
<controls:ShiftInformationItem />
</ListBox>
or:
<StackPanel Name="ShiftInformationPanel">
<controls:ShiftInformationItem />
</StackPanel>
But when I try to add it with C#:
ShiftInformationList.Items.Add(new ShiftInformationItem());
ShiftInformationPanel.Children.Add(new ShiftInformationItem());
it causes the NullReferenceException and says the object I want to add is null.
Does anybody can explain me why?
Im very thankful for all well meaned and helpful answers in advance!
UPDATE:
public partial class HoBusReceptionMain : Window
{
public HoBusReceptionMain()
{
InitializeComponent();
}
private void RibbonWin_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
RibbonTab r = (RibbonTab)e.AddedItems[0];
switch (r.Header.ToString())
{
case "Shift Information":
InitializeShiftInformationTab();
break;
default:
MessageBox.Show(e.AddedItems[0].ToString());
break;
}
}
private void InitializeShiftInformationTab()
{
//here I want to add the new ShiftInformationItem
}
}
UPDATE 2:
Thanks all comments, it Shows up, that my list || Panel is null ... But both is included in the main window (above HoBusReceptionMain)
I use Ribbons in my application, and when a ribbon tab is selected or loaded the RibbonWin_SelectionChanged Event is fired ... The list or Panel defined below the ribbon definitions
I suspect you are adding that before InitializeComponent() gets called.
Move the code of adding below InitializeComponent() and it will work like it did from XAML. Issue is controls were not initialized before InitializeComponent() gets called and hence resulting in NullReferenceException.
public MainWindow()
{
InitializeComponent();
ShiftInformationList.Items.Add(new ShiftInformationItem());
ShiftInformationPanel.Children.Add(new ShiftInformationItem());
// ShiftInformationPanel is null here
}
I have a problem that is driving me crazy, I am working on a project in WPF and I am creating a view.
I was designing a window which contains a "More Options" section, I had even been able to make this section show or hide. This section contained a tabControl which contained a TextBox as bellow:
<TabControl Margin="10,156,12,39" Name="moreTabControl" Grid.Column="1">
<TabItem >
<Grid>
<TextBox Margin="6,6,8,28" Name="myTextBox" />
</Grid>
</TabItem>
</TabControl>
So, in the code behind what I do to show or hide the "More Section" is as following:
public partial class FilterView : System.Windows.Window
{
.....
// Window's height when "more" option are showed
private const int ShowMoreHeight = 386;
/// Window's height when "more" option are hidden
private const int ShowLessHeight = 186;
private bool showMore = false;
private void moreButton_Click(object sender, RoutedEventArgs e)
{
showMore = !showMore;
ResizeWindow();
}
private void ResizeWindow()
{
if (showMore)
{
moreTabControl.Visibility = System.Windows.Visibility.Visible;
moreButton.Content = "<< Less";
MinHeight = ShowMoreHeight;
Height = ShowMoreHeight;
}
else
{
moreTabControl.Visibility = System.Windows.Visibility.Collapsed;
moreButton.Content = "More >>";
MinHeight = ShowLessHeight;
Height = ShowLessHeight;
}
}
......
.....
}
Everything went well until I needed to change the TextBox for a RichTextBox :(, when I run the program and press the "MoreButton" the "more" section is showed as expected but the container window grows a lot to the right!
And I only changed this: <TextBox Margin="6,6,8,28" Name="myTextBox" /> for this: <RichTextBox Margin="6,6,8,28" Name="myRichTextBox" />
Does anyone know what is happening??
Thank you in advance.
I have solved my problem:
Turns out that in my XAML code, my Window had a property called SizeToContent set to "WidthAndHeight", so I changed it to "Manual" and established a value for Width and Height manually.
Hope this helps some else who is experiencing somethig similar.
I am having an issue with using a Listbox in my parent project to display a property defined in a user control. To be more specific, I have created a user control which contains a webbrowsercontrol, and I have defined a property called History which contains the history of urls visited during a browsing session. I also have a parent project, which is linked to this user control, in which I am attempting to bind the History property to a Listbox. The point is for someone to be able to see the history urls in a Listbox defined in this parent project, where the history urls are populated by binding to the user control's History property.
Below is my code outlining what I am trying to do:
User Control FullWebBrowser.xaml.cs
public partial class FullWebBrowser : UserControl
{
//ctr
public FullWebBrowser()
{
InitializeComponent();
//for FullWebBrowser.xaml ContentGrid
ContentGrid.DataContext = this;
}
#region Fields
//The navigation urls of the browser.
private readonly Stack<Uri> _NavigatingUrls = new Stack<Uri>();
//The history for the browser
private readonly ObservableCollection<string> _History = new ObservableCollection<string>();
/// <summary>
/// Gets the History property for the browser.
/// </summary>
public ObservableCollection<string> History
{
get { return _History; }
}
The _NavigatingUrls stack is for the forward and back button implementation, which is working fine, and the _History observablecollection contains the urls from the webbrowsing session shown as follows
//If the navigated uri is not in thehistory, add it
if (!_History.Contains(e.Uri.ToString()))
_History.Add(e.Uri.ToString());
These seem to be working properly, as I have implemented the forward and back buttons and they work ok. The issue is that I cannot properly bind the History property defined in the FullWebBrowser.xaml.cs to my parent project which contains a Listbox. This is shown as follows
HistoryPage.xaml
xmlns:my="clr-namespace:FullBrowserControl;assembly=FullBrowserControl">
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<!--Pivot Control-->
<controls:Pivot Title="QUEST">
<!--Pivot item one-->
<controls:PivotItem Header="today">
<Grid/>
</controls:PivotItem>
<!--Pivot item two-->
<controls:PivotItem Header="week">
<Grid/>
</controls:PivotItem>
<!--Pivot item three-->
<controls:PivotItem Header="all">
<StackPanel>
<ScrollViewer>
<ListBox x:Name="ListBox" ItemsSource="{Binding}"
SelectionChanged="ListBox_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=History}" Height="Auto"/>
<TextBlock Foreground="{StaticResource PhoneSubtleBrush}"
Text="{Binding Modified, Converter={StaticResource DateConverter}}"
Margin="24,0,0,12"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Scrollviewer>
</StackPanel>
<!--<Grid/>-->
</controls:PivotItem>
</controls:Pivot>
</Grid>
Note, the dateconverter is ok. Here I am trying to implement a Listbox which shows the url with a timestamp below it.
The code behind for this parent project page is as follows
Historypage.xaml.cs
public partial class HistoryPage : PhoneApplicationPage
{
//Temporary State
public static readonly Setting<int> CurrentHistoryIndex = new Setting<int>("CurrentHistoryIndex", -1);
private FullWebBrowser browser = new FullWebBrowser();
public HistoryPage()
{
InitializeComponent();
//create new instance of FullWebBrowser user control?
this.browser = new FullWebBrowser();
this.DataContext = browser;
//browser.DataContext = this;
//this.DataContext = browser.History;
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
//Clear the selection so selecting the same item twice in a row will still raise the SelectedChanged event
CurrentHistoryIndex.Value = -1;
this.ListBox.SelectedIndex = -1;
}
void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (ListBox.SelectedIndex >= 0)
{
//navigate to the page containing the user control for the selected item
//how to navigate to Mainpage.xaml and load webbrowsercontrol with selected url??
CurrentHistoryIndex.Value = ListBox.SelectedIndex;
this.NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
}
}
}
So this is my basic implementation. No matter what I try I cannot get the Listbox in Historypage to bind to the History property in the FullWebBrowser user control contained outside of the project, I have referenced the FullWebBrowser control using the references option in the solution explorer, in a using declaration at the top of Historypage.xaml.cs, and by an xmlns statement at the top of HistoryPage.xaml
Any assistance with how this may be accomplished would be greatly appreciated! I have been working on this for a couple weeks and cannot find the solution anywhere, even prowling other's posts. I must implement this solution ASAP! thanks for all your help in advance. Please include code to accomplish this, it would help so much to see how this is implemented for future reference!
The DataContext of Your ListBox is FullWebBrowser, therefore your binding should be as follows:
<ListBox x:Name="ListBox" ItemsSource="{Binding Path=History}"/>
To solve this kind of problem yourself try debugging, breakpoint your code then inspect the DataContext properties of the various elements in your UI.