I'm trying to access a winform from the wpf usercontrol that is sitting on it. I realize that there is an ElemetHost that is automatically created when you drop a WPF usercontrol onto a winform but I'm not sure how to get something simple to work. For instance:
If I have a button on the WPF control, how could I get it to close the winform that the usercontrol is sitting on?
Thanks
Form1 Loaded
this.elementHost1.Child = new UserControl1() { Tag = this };
XAML of WpfUserControl
<UserControl x:Class="WindowsFormsApp7.UserControl1"
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:WindowsFormsApp7"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid Background="DarkSlateGray">
<Button Name="btnClose" Width="100" Height="25" Content="Close" Click="BtnClose_Click" />
</Grid>
</UserControl>
btnClose Click of WpfUserControl
private void BtnClose_Click(object sender, RoutedEventArgs e)
{
Form1 form = (Form1)this.Tag;
form.Close();
}
Use Tag property of WPFUserControl as a refence holder.
Related
I'm trying to navigate to a different page from my MainWindow in a WPF application. The MainWindow just has some welcome text and I want it to move to another page after 4 seconds. The timer is working just fine but I am getting an error that says,
"Error 1 'UbiTutorial.MainWindow' does not contain a definition for 'Frame' and no extension method 'Frame' accepting a first argument of type 'UbiTutorial.MainWindow' could be found (are you missing a using directive or an assembly reference?) c:\users\thomas\documents\visual studio 2012\Projects\UbiTutorial\UbiTutorial\MainWindow.xaml.cs 54 22 UbiTutorial"
in my method
if (introTime > 4)
{
this.Frame.Navigate(typeof(Touch));
}
Visual Studio is complaining about the Frame part of it.
Frame is a Control Type not a Control Instance, without seeing your Xaml I have no idea of what the name of the Frame that you added is, it probably defaulted to frame1 that is what you will need to use to access the Navigate Method. Hopefully this will give you an idea.
MainWindow.xaml
<Window x:Class="WpfApplication1.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>
<Frame Height="100" HorizontalAlignment="Left" Margin="10,10,0,0" Name="frame1" VerticalAlignment="Top" Width="200" />
</Grid>
</Window>
MainWindow.xaml.cs
using System.Windows.Threading;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
DispatcherTimer introTime = new DispatcherTimer();
public MainWindow()
{
InitializeComponent();
introTime.Interval = TimeSpan.FromSeconds(4);
introTime.Tick += new EventHandler(introTime_Tick);
introTime.Start();
}
void introTime_Tick(object sender, EventArgs e)
{
//this.frame1.Navigate(new Uri(#"http://www.google.com"));
this.frame1.Navigate(new UserControl1());
}
}
}
UserControl1.xaml
<UserControl x:Class="WpfApplication1.UserControl1"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300" Background="Red" >
<Grid>
</Grid>
</UserControl>
Parent window freezes when I open a window on click of a button. It works properly again if I minimize and maximize parent window again.
System configuration
Windows 7, 64 bit OS.
.Net framework 3.5 SP1
Visual studio express 2008
Following is the simple application where I can reproduce this issue.
Window1.xaml
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<StackPanel>
<TextBox Width="200"/>
<Button Content="click" Click="Button_Click"/>
</StackPanel>
</Window>
Window1.xaml.cs
namespace WpfApplication1 {
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window {
public Window1() {
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e) {
(new Window2()).ShowDialog();
}
}
}
Window2.xaml
<Window x:Class="WpfApplication1.Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window2" Height="300" Width="300" WindowStyle="ToolWindow">
<Grid>
</Grid>
</Window>
It works fine if I remove WindowStyle="ToolWindow" !!!!!!
App.xaml
<Application x:Class="WpfApplication1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Window1.xaml">
<Application.Resources>
</Application.Resources>
</Application>
The call to ShowDialog() is blocking your GUI thread.
See this question for alternative approaches which don't block the main thread.
Use Show instead of ShowDialog which opens a window in Modal mode
If you use ShowDialog() try set Window1 as Owner for Window2:
Window2 w = new Window2 { Owner = this };
w.ShowDialog();
Show instead of show dialog will work, but maybe you intention was something more like a child window see http://wpftoolkit.codeplex.com/wikipage?title=ChildWindow&referringTitle=Home for details.
I'm new in WPF and I'm creating a control. This control contains a DataGrid and some other WPF controls.
I created my Control as below:
<UserControl x:Class="MyControls.MyControl"
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"
mc:Ignorable="d"
d:DesignHeight="329" d:DesignWidth="535" >
<Grid>
<DataGrid Margin="6,25,6,35" Name="dataGrid" SelectionUnit="CellOrRowHeader" x:FieldModifier="public" HeadersVisibility="All"/>
<OtherControl HorizontalAlignment="Left" x:Name="otherControl" Height="34" VerticalAlignment="Bottom" Width="523" x:FieldModifier="private"/>
<Label Content="caption" Height="24" HorizontalAlignment="Left" Name="captionLabel" VerticalAlignment="Top" Foreground="#FF2626D1" x:FieldModifier="private"/>
</Grid>
</UserControl>
So, everything goes well so far, then I create a container UserControl which has in it my control created previously:
<UserControl x:Class="MyContainers.MyContainer"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300" xmlns:my="clr-namespace:MyControls">
<Grid>
<my:MyControl>
</my:MyControl>
</Grid>
What I can not do is the following:
<my:MyControl>
<my:MyControl.dataGrid>
</my:MyControl.dataGrid>
</my:MyControl>
I previously set the datagrid's property of FieldModifier as public in order to get access to it in another xaml, but it raises an error from visual studio.
I need to "expose" my dataGrid in order to be able to add columns and their styles.
I would like to be able to do something like this:
<my:MyControl.dataGrid.Columns >
<DataGridTextColumn />
<DataGridTextColumn />
...
<DataGridTextColumn />
</my:MyControl.dataGrid.Columns>
So, is not enough to set the datagrid's property of FieldModifier as public?
Do I need to do something else? How can I achieve this? Is this even possible?
I hope someone can help me. Thank you in advance.
You cannot access the child DataGrid as MyControl.dataGrid -- MyControl has no property named "dataGrid".
You might try adding a dependency property of type ObservableCollection<DataGridColumn> to MyControl, and modify the dataGrid columns whenever that collection changes.
EDIT:
Whipped together a quick example for you:
UserControl code:
public partial class UserControl1 : UserControl
{
public static readonly DependencyProperty ColumnsProperty = DependencyProperty.Register("Columns", typeof(ObservableCollection<DataGridColumn>), typeof(UserControl1));
public ObservableCollection<DataGridColumn> Columns
{
get { return (ObservableCollection<DataGridColumn>)GetValue(ColumnsProperty); }
set { SetValue(ColumnsProperty, value); }
}
public UserControl1()
{
Columns = new ObservableCollection<DataGridColumn>();
Columns.CollectionChanged += (s, a) =>
{
dataGrid.Columns.Clear();
foreach (var column in this.Columns)
dataGrid.Columns.Add(column);
};
InitializeComponent();
}
}
UserControl xaml:
<UserControl x:Class="WpfApplication1.UserControl1"
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">
<Grid>
<DataGrid x:Name="dataGrid" AutoGenerateColumns="False"/>
</Grid>
so you can use it like:
<Grid>
<l:UserControl1>
<l:UserControl1.Columns>
<DataGridTextColumn Header="Col1"/>
<DataGridTextColumn Header="Col2"/>
</l:UserControl1.Columns>
</l:UserControl1>
</Grid>
I'm trying to do a simplest thing: bind a View to ViewModel, but for some reason it doesn't work.
I've got MainWindow:
<Window x:Class="ImagesToAmazon.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"
xmlns:v="clr-namespace:ImagesToAmazon.View"
xmlns:vm="clr-namespace:ImagesToAmazon.ViewModel" >
<Window.Resources>
<DataTemplate DataType="{x:Type vm:MainViewModel}">
<v:MainControl />
</DataTemplate>
</Window.Resources>
<Grid>
</Grid>
MainViewModel:
namespace ImagesToAmazon.ViewModel{
public class MainViewModel {
}}
MainControl:
<UserControl x:Class="ImagesToAmazon.View.MainControl"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<StackPanel>
<Button Content="Button" Height="23" Name="button1" Width="75" />
</StackPanel>
Also, I override App.OnStartup to set MainWindow context:
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
var window = new MainWindow();
var viewModel = new ViewModel.MainViewModel();
window.DataContext = viewModel;
window.Show();
}
}
All compiles, but I don't see my button from MainControl.
Any clues?
In the main window, replace the Grid with
<ContentControl Content="{Binding }" />
This will attempt to use the current DataContext as the content of the control, and it will use the DataTemplate u defined earlier.
What could be a correct way to assign parameters to objects in WPF window (objects as simple as rectangles) and pass them to event handlers on mouse clicks?
Thank you.
Here's one way:
Put this in the Window:
public static readonly RoutedUICommand clickCommand = new RoutedUICommand("TheCommand", "TheCommand", typeof(Window1));
void ClickRectangle(object sender, ExecutedRoutedEventArgs e)
{
if (e.Parameter == null)
return;
MessageBox.Show("parameter = " + e.Parameter.ToString());
}
Then in the XAML:
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="clr-namespace:WpfApplication1"
Title="Window1" Height="300" Width="300">
<Grid>
<Rectangle Margin="50" Fill="Blue">
<Rectangle.CommandBindings>
<CommandBinding
Command="my:Window1.clickCommand" Executed="ClickRectangle" />
</Rectangle.CommandBindings>
<Rectangle.InputBindings>
<MouseBinding MouseAction="LeftClick"
Command="my:Window1.clickCommand" CommandParameter="123" />
</Rectangle.InputBindings>
</Rectangle>
</Grid>
</Window>