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 ?
Related
I have called a WPF form from Windows.Forms programatically. Now the concern here is i need to close the wpf form when clicked the button inside the wpf form but its not happening and staying in the WPF screen only. Any idea how do we close the wpf form and proceed for further steps.
Form dialog = new Form();
dialog.Width = 200;
dialog.Height = 100;
ctrlHost = new ElementHost();
ctrlHost.Dock = DockStyle.Fill;
prompt.Controls.Add(ctrlHost);
wpfControl = new User_Control();
wpfControl.InitializeComponent();
ctrlHost.Child = wpfControl;
wpfControl.dialogResult = dialog.ShowDialog();
If you see my above code i have created the form in the run time and assigned the wpf to that. But after showdialog i am struck in that screen and not able to come out. So need a code to close the wpf and come out of that form.
Any idea how do we come out of the wpf back. As i have created some buttons in WPF and need to come out when clicked on that button to the next code where i have loaded the form.
Well a solution could be manage a custom Form and an event.
I've created a simple test project with:
FormMain: the main form with a button to open a HostForm
FormHost: a form with the ElementHost where the wpf usercontrol will be showed
TestControl: the wpf control to show on the ElementHost
Code of the FormMain
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var dialog = new FormHost();
dialog.Width = 200;
dialog.Height = 100;
var result = dialog.ShowDialog();
}
}
Has you can see i just called the FormHost and wait its result
Code of the FormHost:
public partial class FormHost : Form
{
public FormHost()
{
InitializeComponent();
elementHost1.Dock = DockStyle.Fill;
var wpfControl = new TestControl();
wpfControl.InitializeComponent();
//Code your events management here
wpfControl.Close += WpfControl_Close;
elementHost1.Child = wpfControl;
}
private void WpfControl_Close(DialogResult result)
{
//Manage the result as you like
DialogResult = result;
Close();
}
}
In the FormHost I've managed the initilization of the WPF control also the management of events.
Xaml code of the TestControl
<UserControl x:Class="TestHost.TestControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:TestHost"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<Button Content="Ok" Width="60" VerticalAlignment="Center" HorizontalAlignment="Center" Click="Ok_Click" Margin="10"/>
<Button Content="Cancel" Width="60" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="10" Click="Cancel_Click"/>
</StackPanel>
Code behind of the TestControl:
public partial class TestControl : System.Windows.Controls.UserControl
{
public event Action<DialogResult> Close;
public TestControl()
{
InitializeComponent();
}
private void Ok_Click(object sender, RoutedEventArgs e)
{
Close?.Invoke(DialogResult.OK);
}
private void Cancel_Click(object sender, RoutedEventArgs e)
{
Close?.Invoke(DialogResult.Cancel);
}
}
As you can see i've created an event and managed on the parent Form of the ElementHost, than I've closed the FormHost and setted its DialogResult.
"I have two or more dockingManager, and I want every FloatingWindows to be dockable on every dockingManager."
floatingWindows must be created within a dockingManager, but it must be possible to dock it into another separate docking manager.
so when I do drag inside the first dockingManager the default positions are displayed. (4 icons: top, left, bottom, right)
but when I drag the floatingWindows into the second dockingManager nothing appears.
<Grid>
<avalonDock:DockingManager x:Name="dock1" Width="300" Height="200">
<avalonDockLayout:LayoutRoot>
<avalonDockLayout:LayoutRoot.FloatingWindows>
<avalonDockLayout:LayoutAnchorableFloatingWindow>
<avalonDockLayout:LayoutAnchorablePaneGroup DockHeight="100" DockWidth="100">
<avalonDockLayout:LayoutAnchorablePane x:Name="_LayoutAnchorablePane" FloatingHeight="150" FloatingWidth="150">
<avalonDockLayout:LayoutAnchorableTitle="FloatingWindow">
<Grid Background="CornflowerBlue">
</Grid>
</avalonDockLayout:LayoutAnchorable>
</avalonDockLayout:LayoutAnchorablePane>
</avalonDockLayout:LayoutAnchorablePaneGroup>
</avalonDockLayout:LayoutAnchorableFloatingWindow>
</avalonDockLayout:LayoutRoot.FloatingWindows>
</avalonDockLayout:LayoutRoot>
</avalonDock:DockingManager>
<avalonDock:DockingManager x:Name="dock2" Width="300" Height="200" >
<avalonDockLayout:LayoutRoot>
</avalonDockLayout:LayoutRoot>
</avalonDock:DockingManager/>
I also tried to force it from code, but it does not work!
LayoutAnchorable mAnchorableChilder=null;
public MainWindow()
{
InitializeComponent();
mAnchorableChilder = new LayoutAnchorable();
mAnchorableChilder.Title = "Sample dynamic avalondock";
_LayoutAnchorablePane.Children.Add(mAnchorableChilder);
Grid grid = new Grid { Background = new SolidColorBrush(Colors.Green) };
mAnchorableChilder.Content = grid;
}
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.
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.