Why destination Image image1 is not changing after I drag Image img2 on it.
My XAML Code:
<Window x:Class="WpfApplication4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="463.42" Width="861.214">
<Grid>
<Image Name="image1" Margin="291,10,297,94" AllowDrop="True" Source="C:/img/0.png" Drop="img1_Drop"/>
<Button x:Name="btn1" Content="merge" HorizontalAlignment="Left" Margin="753,392,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
<Button Content="add" HorizontalAlignment="Left" Margin="658,392,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/>
</Grid>
Here is C# code:
private void img1_Drop(object sender, DragEventArgs e)
{
image1.Source = (System.Windows.Media.ImageSource)e.Data.GetData(typeof(System.Windows.Media.ImageSource));
//image1.Source = (BitmapImage)e.Data.GetData(DataFormats.Bitmap);
}
Nothing happens, how to solve this problem ?
You can read the image directly from dropped filename info like this:
if(e.Data.GetDataPresent(DataFormats.FileDrop))
{
var filename = ((string[])e.Data.GetData(DataFormats.FileDrop))[0];
image1.Source = new BitmapImage(new Uri(filename));
}
Related
EDIT : My program doesn't start after adding the Background, and an icon to the program I added:
Icon="/assets/icon.ico"
Background="/assets/background.svg">
I checked if I had made a mistake but in my code everything is OK finally according to the IDE and according to me the beginner
Class MainWindow.xaml.cs :
using Avalonia.Controls;
using Avalonia.Interactivity;
namespace AvaloApp;
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
}
private void startButton(object sender, RoutedEventArgs e) {
Button start = new Button();
}
private void stopButton(object sender, RoutedEventArgs e) {
Button stop = new Button();
}
private void hotkeyButton(object sender, RoutedEventArgs e) {
Button hotkey = new Button();
}
}
Class MainWindow.xaml :
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="AvaloApp.MainWindow"
Title="AvaloApp"
Icon="/assets/icon.ico"
Background="/assets/background.svg">
<Grid>
<Button Grid.Column="1" Grid.Row="0" x:Name="start" Content="Start" Click="startButton" Margin="5" HorizontalContentAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center" Width="100" Height="50"/>
<Button Grid.Column="2" Grid.Row="0" x:Name="stop" Content="Stop" Click="stopButton" Margin="280,5,5,5" HorizontalContentAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center" Width="100" Height="50"/>
<Button Grid.Column="3" Grid.ColumnSpan="2" Grid.Row="1" x:Name="hotkey" Content="Hotkey" Click="hotkeyButton" Margin="5,140,5,5" HorizontalContentAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center" Width="200" Height="50"/>
</Grid>
</Window>
I tried to debug the program, it says that it does not find the icon, except that it is placed here in the project structure: https://i.imgur.com/Z7WTJmW.png (in the assets folder) and in my code I have specified the location
Even removing the / before assets still doesn't find the file, and surely prevents the project from running properly
The result is the same when I put the images like this: https://i.imgur.com/AdX5LWe.png
I try to develop WebBrowser on c#, wpf and CefSharp for experience.
I use TabControl for tabs in WebBrowser.
So i add webbrowser to tabItem.Content but it dont showing up.
public partial class MainWindow : Window
{
ChromiumWebBrowser webBrowser;
public MainWindow()
{
//Browser init
CefSettings settings = new CefSettings();
Cef.Initialize(settings);
InitializeComponent();
textboxURL.Text = "https://www.google.com";
webBrowser = new ChromiumWebBrowser();
webBrowser.Load(textboxURL.Text);
tabItem1.Content = webBrowser;
}
//App Shutdown logic
private void Exit(object sender, StartupEventArgs e)
{
Cef.Shutdown();
}
}
This is XAML code. As you see i use DockPanel for topbar with URL and buttons.
And TabControl for tabs with browser. For now i use only one webBrowser for one tab.
Window x:Class="WebBrowser.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WebBrowser"
xmlns:cefSharp="clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf"
mc:Ignorable="d"
Title="WebBrowser" Height="362" Width="699" WindowState="Maximized" WindowStartupLocation="CenterScreen">
<StackPanel HorizontalAlignment="Stretch" Margin="0,0,0,-1" Height="auto" VerticalAlignment="Stretch" >
<DockPanel VerticalAlignment="Top">
<Button x:Name="btnHome" Content="Home" Margin="2" DockPanel.Dock="Left"/>
<Button x:Name="btnBack" Content="Back" Margin="2" DockPanel.Dock="Left"/>
<Button x:Name="btnNext" Content="Next" Margin="2" DockPanel.Dock="Left"/>
<Label x:Name="labelHttp" Content="HTTP" DockPanel.Dock="Left"/>
<Button x:Name="btnMenu" Content="Menu" Margin="2" DockPanel.Dock="Right"/>
<Button x:Name="btnGo" Content="Go" Margin="2" DockPanel.Dock="Right"/>
<Button x:Name="btnRefresh" Content="Refresh" Margin="2" DockPanel.Dock="Right"/>
<TextBox x:Name="textboxURL" Margin="2"/>
</DockPanel>
<TabControl x:Name="tabControl1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0,5,0,0" Height="299">
<TabItem Header="TabPage" Name="tabItem1">
</TabItem>
</TabControl>
</StackPanel>
Ok, i try to use Address property then init browser and its load my url!
So answer is dont use webBrowser.Load(textboxURL.Text); on new element but use webBrowser.Address = textBoxURL.Text;
Thanks everyone!
--With CefSharp you can with this code--
public ChromiumWebBrowser drv;
string url = "https://www.example.com";
public void syhmhfzaddtab()
{
TabPage tb = new TabPage();
CefSettings settings = new CefSettings();
Cef.Initialize(settings);
drv = new ChromiumWebBrowser(url);
drv.Parent = tb;
tabControl1.Controls.Add(tb);
drv = new ChromiumWebBrowser(url);
drv.Dock = DockStyle.Fill;
}
Note:%100 a working code
Why does this no work? The Stack Panel has default settings Buttons shouldn't be out of border.
void AddWrapPanel() {
WrapPanel myWrapPanel = new WrapPanel();
myWrapPanel.Background = System.Windows.Media.Brushes.Azure;
myWrapPanel.Orientation = Orientation.Horizontal;
myWrapPanel.Width = 200;
myWrapPanel.HorizontalAlignment = HorizontalAlignment.Left;
myWrapPanel.VerticalAlignment = VerticalAlignment.Top;
// Define 3 button elements. The last three buttons are sized at width
// of 75, so the forth button wraps to the next line.
Button btn1 = new Button();
btn1.Content = "Button 1";
btn1.Width = 200;
Button btn2 = new Button();
btn2.Content = "Button 2";
btn2.Width = 75;
// Add the buttons to the parent WrapPanel using the Children.Add method.
myWrapPanel.Children.Add(btn1);
myWrapPanel.Children.Add(btn2);
this.stackPanel1.Children.Add(myWrapPanel);
}
private void button1_Click(object sender, RoutedEventArgs e)
{
AddWrapPanel();
}
here the XAML
<Window x:Class="AmpelThingy.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="349,276,0,0" Name="button1" VerticalAlignment="Top" Width="75" />
<StackPanel Height="214" HorizontalAlignment="Left" Margin="32,33,0,0" Name="stackPanel1" VerticalAlignment="Top" Width="392" />
</Grid>
</Window>
its a simle problem i dont know what more details i can write
Here you have created a method "button1_Click" but how does WPF know when it should actuallly call it? So, you have to hook buttons click event to "button1_Click" method. You can do it using Click="button1_Click" property. Now your XAML will look like :
<Grid>
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="349,276,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
<StackPanel Height="214" HorizontalAlignment="Left" Margin="32,33,0,0" Name="stackPanel1" VerticalAlignment="Top" Width="392" />
</Grid>
Hope this will help you..!
I am new to WPF and I want to create a WPF application with 5buttons. On the click of each button I want a content to be displayed on another panel. Right now I just want different images to be displayed on my right side panel on the button clicks.
Here's my XAML code:
<Window x:Class="GridButton.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MyFirstApp" Height="350" Width="525" Loaded="Window_Loaded">
<Viewbox Stretch="Fill" StretchDirection="Both">
<DockPanel>
<StackPanel DockPanel.Dock="left" Margin="5" Width="Auto" VerticalAlignment="Center" Height="Auto">
<Button Content="1" Name="button2" Click="button2_Click">
</Button>
<Button Content="2" Name="button1" Click="button1_Click_1">
</Button>
<Button Content="3" Name="button3" Click="button3_Click">
</Button>
<Button Content="4" Name="button4" Margin="5">
</Button>
<Button Content="5" Name="button5" Margin="5" Click="button5_Click_1">
</Button>
</StackPanel>
<StackPanel DockPanel.Dock="Right">
<Image Name="img1" Source="Blue Hills.jpg" Stretch="Uniform" Visibility="Hidden" ImageFailed="Image_ImageFailed" Height="257" />
</StackPanel>
</DockPanel>
And my xaml.cs file contains code to display image:
private void button2_Click(object sender, RoutedEventArgs e)
{
img1.Visibility = Visibility.Visible;
}
I could get only this far.
You can set the Source property of the Image control in code:
private void buttonx_Click(object sender, RoutedEventArgs e)
{
string path = ... // path to image file here
img1.Source = new BitmapImage(new Uri(path));
}
You could easily reuse the same Click handler for all Buttons and check which one was pressed:
private void Button_Click(object sender, RoutedEventArgs e)
{
Button button = sender as Button;
string path = null;
if (button == button1)
{
path = ... // path to image file 1 here
}
else if ...
if (path != null)
{
img1.Source = new BitmapImage(new Uri(path));
}
}
If you want to remove the a child Panel (or other control) from a parent Panel and add another one, you would have to modify the Panel's Children property:
<StackPanel Name="parent">
<StackPanel Name="child" />
</StackPanel>
parent.Children.Remove(child);
parent.Children.Add(...); // some other control here
This approach would usually make sense if you wanted to create child panels dynamically. If you want to declare everything in XAML you may put all child panels in a Grid and change their visibility as you did already.
However, you might also change the ZIndex attached property.
<Grid>
<StackPanel Name="child1">
</StackPanel>
<StackPanel Name="child2">
</StackPanel>
<StackPanel Name="child3">
</StackPanel>
</Grid>
child3 is topmost by default, but now you can set ZIndex to some value > 0 to make another child topmost:
private void Button_Click(object sender, RoutedEventArgs e)
{
...
// reset ZIndex on previous topmost panel to 0 before
Panel.SetZIndex(child1, 1);
}
Or completely omit the Button/Grid/Panel design and use a TabControl.
I got a strange problem. I have a tabcontrol and 3 tabs. On every tab i got a webbrowser control on it. They all navigate to a website. But it only navigates if you're actually looking at the webbrowser control. So having it minimized on taskbar or systray, wont make it navigate to a website.
Why is that? How can i change this behavior?
[EDIT]
This only seems to happen when i startup the app. After it got 'focus' or a 'look at', this doesn't happen anymore.
Some more info, the navigating happens from a different thread than the UI-thread.
[/EDIT]
[3nd EDIT]
Here is a test case:
XAML code:
<Window x:Class="WPFWebbrowserFocusTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="453" Width="755">
<Grid>
<TabControl Height="390" HorizontalAlignment="Left" Margin="12,12,0,0" Name="tabControl1" VerticalAlignment="Top" Width="709">
<TabItem Header="tabItem1" Name="tabItem1">
<Grid>
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="18,17,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
</Grid>
</TabItem>
<TabItem Header="tabItem2" Name="tabItem2">
<Grid>
<WebBrowser Height="352" HorizontalAlignment="Left" Margin="0,6,0,0" Name="webBrowser1" VerticalAlignment="Top" Width="693" Navigated="webbrowser_Navigated" LoadCompleted="webbrowser_LoadCompleted" />
</Grid>
</TabItem>
<TabItem Header="tabItem3" Name="tabItem3">
<Grid>
<WebBrowser Height="346" HorizontalAlignment="Left" Margin="6,6,0,0" Name="webBrowser2" VerticalAlignment="Top" Width="687" Navigated="webbrowser_Navigated" LoadCompleted="webbrowser_LoadCompleted" />
</Grid>
</TabItem>
<TabItem Header="tabItem4" Name="tabItem4">
<Grid>
<WebBrowser Height="346" HorizontalAlignment="Left" Margin="10,10,0,0" Name="webBrowser3" VerticalAlignment="Top" Width="687" Navigated="webbrowser_Navigated" LoadCompleted="webbrowser_LoadCompleted" />
</Grid>
</TabItem>
<TabItem Header="tabItem5" Name="tabItem5">
<Grid>
<WebBrowser Height="346" HorizontalAlignment="Left" Margin="10,10,0,0" Name="webBrowser4" VerticalAlignment="Top" Width="687" Navigated="webbrowser_Navigated" LoadCompleted="webbrowser_LoadCompleted" />
</Grid>
</TabItem>
</TabControl>
</Grid>
Here is the code behind file:
public MainWindow()
{
InitializeComponent();
}
private void webbrowser_Navigated(object sender, NavigationEventArgs e)
{
this.SuppressScriptErrors((WebBrowser)sender, true);
}
private void webbrowser_LoadCompleted(object sender, NavigationEventArgs e)
{
WebBrowser wb = (WebBrowser)sender;
if (e.Uri.AbsoluteUri != wb.Source.AbsoluteUri)
return;
}
public void SuppressScriptErrors(System.Windows.Controls.WebBrowser wb, bool Hide)
{
FieldInfo fi = typeof(System.Windows.Controls.WebBrowser).GetField(
"_axIWebBrowser2", BindingFlags.Instance | BindingFlags.NonPublic);
if (fi != null)
{
object browser = fi.GetValue(wb);
if (browser != null)
{
browser.GetType().InvokeMember("Silent", BindingFlags.SetProperty, null, browser, new object[] { Hide });
}
}
}
private void button1_Click(object sender, RoutedEventArgs e)
{
this.webBrowser1.Navigate("http://www.google.com");
this.webBrowser2.Navigate("http://www.google.com");
this.webBrowser3.Navigate("http://www.google.com");
this.webBrowser4.Navigate("http://www.google.com");
}
How to reproduce:
Put a breakpoint inside webbrowser_LoadCompleted. Then press the button which is located on the first tabpage of the tabcontrol.
Dont go to the next tabpage yet, wait a coupled of seconds, like 15 or so.
Then go to tabitem2 or 3/4/5. You'll see that the page just got loaded and the webbrowser_LoadCompleted event got fired.
Here's a code fragment in WPF that works. Once you click the button, it minimizes the application, and after 2 seconds calls navigate to all browsers while the window is minimized. Pages are loaded in all tabs regardless of window state or tab focus.
Make sure are calling Navigate inside a Dispatcher.Invoke. You can't make UI changes in WPF from a different thread unless you call the dispatcher. That might be a problem.
My example below calls the navigation from a different thread.
<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
Title="MainWindow" Height="350" Width="525"
StateChanged="Window_StateChanged">
<Grid>
<TabControl Height="225" HorizontalAlignment="Left" Margin="12,12,0,0" Name="tabControl1" VerticalAlignment="Top" Width="491">
<TabItem Header="tabItem1">
<WebBrowser Height="189" Name="webBrowser1" Width="479" />
</TabItem>
<TabItem Header="tabItem2">
<WebBrowser Height="185" Name="webBrowser2" Width="466" />
</TabItem>
<TabItem Header="tabItem3">
<WebBrowser Height="187" Name="webBrowser3" Width="434" />
</TabItem>
</TabControl>
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="116,268,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="236,268,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
</Grid>
</Window>
private void button1_Click(object sender, RoutedEventArgs e)
{
this.WindowState = System.Windows.WindowState.Minimized;
}
private void Window_StateChanged(object sender, EventArgs e)
{
if (this.WindowState == System.Windows.WindowState.Minimized)
{
new Thread((state) =>
{
Thread.Sleep(TimeSpan.FromSeconds(2));
this.Dispatcher.Invoke(new Action(() =>
{
webBrowser1.Navigate(textBox1.Text);
webBrowser2.Navigate(textBox1.Text);
webBrowser3.Navigate(textBox1.Text);
}), null);
}).Start();
}
}
It seems that this is the behavior of this control in WPF according to the docs.