I am very new to C# and I'm trying to initialize a XAML user control window when I start my scripting program. It's just a basic window with textboxes, and comboboxes. The XAML code and C# code are listed below respectively. Since I have Express, I am unable to use the MVVM light toolkit. I am also using VS2010 because that is what the original code for this program was. The VMS.TPS.Common.Model.API and Types are dll's used for this particular program. Keep in mind that the C# code has to have this basic skeleton, otherwise it won't work. The 'public void Execute' portion is where I need to code.
<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="426" d:DesignWidth="736">
<Grid Margin="10" Width="702" HorizontalAlignment="Center">
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VMS.TPS.Common.Model.API;
using VMS.TPS.Common.Model.Types;
using System.Windows;
using System.Windows.Forms;
using WpfApplication1;
namespace VMS.TPS
{
public class Script
{
public Script()
{
}
public void Execute(ScriptContext context, System.Windows.Window window)
{
}
}
}
It should be very simple if i understood it right :
UserControl1 testUsrCtrl = new UserControl1();
And then set required properties.
Make sure you have all the references in place.
Related
I have a c# project already working a main Window but when I created a New Window and use
Views.Form Form = new Views.Form();
Form.ShowDialog();
The page doe's open but it not showing the xml I deseigned and I think that the problem is in namespaces and derectories but havin hard time troubleshooting the proble so this is my solution explorer
Properities
References
Model
Acteur.cs
Tache.cs
ViewModels
ActeurViewModel.cs
TacheViewModel.cs
Views
Form.xaml
MainWindow.xaml
App.xaml
Model.edmx
and this is the Form Window code :
.cs file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace WpfApplication2.Views
{
/// <summary>
/// Interaction logic for Form.xaml
/// </summary>
public partial class Form : Window
{
public Form()
{
}
}
}
.xaml file
<Window x:Class="WpfApplication2.Form"
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:WpfApplication2"
mc:Ignorable="d"
Title="Form" Height="300" Width="300">
<Grid Background="#FF142683">
<Label Name="Test" Content="this is a test" FontSize="43"/>
</Grid>
</Window>
the MainWindow Calling
private void AddButtonClicked(object sender, RoutedEventArgs e)
{
Views.Form Form = new Views.Form();
Form.ShowDialog();
}
Have you delete the method "InitializeComponent();" in the constructor?
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
}
This method is created automatically by vs, and it will initialize those controls that you used in the XAML.cs.
Since you have deleted that method, that's the reason why you can not see your controls.
Just try to create a new window and you will find that method that I mentioned.
I am fairly new to WPF and I am having a problem with inheriting from a user control.
I created a User Control and now I need to inherit from that control and add some more functionality.
Has anyone does this sort of thing before? Any help would be greatly appreciated.
Thank you
Well .. you create your base control
public abstract class BaseUserControl : UserControl{...}
then in the XAML file :
<Controls:BaseUserControl x:Class="Termo.Win.Controls.ChildControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Controls="clr-namespace:Namespace.Of.Your.BaseControl">
And that should work.
EDIT: Hmm.. this example is useful when you have a base control without XAML and then inherit from it. The other way around(from a base control with Xaml) - I'm not sure how you can go about it.
EDIT2: Apparently from this post + comments i take that what you want might not be possible.
AFAIK you cannot inherit the xaml, you can only inherit the code behind.
We recently encountered the same problem on our project. The way we ended up solving our problem was to create a usercontrol and adding it to the "child" usercontrol.
If that doesnt work/help take a look at this:
https://web.archive.org/web/20200815091447/http://geekswithblogs.net/lbugnion/archive/2007/03/02/107747.aspx[1]
I may have a bit of a solution: Composition instead of inheritance - I have come up with control, that has 'content slots' assignable from outside through databinding, look at my SO thread.
Example of use:
<UserControl ... >
<!-- My wrapping XAML -->
<Common:DialogControl>
<Common:DialogControl.Heading>
<!-- Slot for a string -->
</Common:DialogControl.Heading>
<Common:DialogControl.Control>
<!-- Concrete dialog's content goes here -->
</Common:DialogControl.Control>
<Common:DialogControl.Buttons>
<!-- Concrete dialog's buttons go here -->
</Common:DialogControl.Buttons>
</Common:DialogControl>
<!-- /My wrapping XAML -->
</UserControl>
Together with some handling code in codebehind it would be a nice base component for dialog windows.
You cannot inherit the xaml code it self. However creating an abstract class of the codebehind, will allow you to edit in code behind, from a derived class object.
Xaml Code: { Window1.xaml }
<Window
x:Class="WPFSamples.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="auto" Width="256" Title="WPF Sameples">
<Grid>
<Button x:Name="Button1" VerticalAlignment="Center" HorizontalAlignment="Center" Content="Click Me"/>
</Grid>
</Window>
CodeBehind: { Window1.xaml.cs }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WPFSamples
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public abstract partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
}
}
Derived Class : { DisabledButtonWindow.cs }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WPFSamples
{
public sealed class DisabledButtonWindow : Window1
{
public DisabledButtonWindow()
{
Button1.IsEnabled = false;
}
}
}
although you cannot inherit from the wpf source it self, you are able to use this "Window1" control as a template for all other derived controls.
You can accomplish this by using a delegate.
Essentially, you need to create an interface (YourInterface) which wraps up the functionality you want, then make both the user control and your class implement that interface.
Next, make sure the user control has a reference to an object of type IYourInterface, so that when your user control attempts to invoke a method of the Interface, it calls your class' method.
Because both the user control and class implement the same interface, they can be seen as the same kind of object - meaning you can put them both into a collection of objects of type IYourInterface. This should give you the behavior you want.
In ASP.NET I use this technique often, by having my classes inherit from abstract class ancestors. I don't understand why WPF doesn't support this. :(
I think that you can do this but that you will have to redefine any functions and possibly some other stuff that you reference in the xaml in the child class.
IE if you have a button click event that you subscribe to in the base class xaml you will need override the button click in the child class and call the base class button click event.
Not one hundred percent sure of the the details since it's my coworkers code that I'm drawing from but thought this would give a start to anyone looking to implement this in the future.
I have looked through past questions on this issue to no avail.
I've just created a new WPF project in VS 2013. I go to Add Reference, and select System.Windows.Form. It adds. Great!
However, the appropriate tools are still greyed out in the toolbox. Yes, auto-update toolbox is on. I've shown all. I've restarted VS and rebuilt my solution. I've added using System.Windows.Forms; to my MainWindow.xaml.cs file.
At this, I only have the bare bones code because this is a completely new project that I haven't touched yet.
What am I missing?? I've tried dragging the .dll file to the toolbox, tools are still greyed. Is there a piece of code I'm missing somewhere?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Forms;
namespace sub20tool3
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}
And:
<Window x:Class="sub20tool3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
xmlns:local="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
Title="MainWindow" Height="350" Width="525">
<Grid>
</Grid>
</Window>
You need a WindowsFormHost element to hold winforms controls in a WPF project. The WindowsFormHost also needs a reference to WindowsFormIntegration.
Here's a good tutorial on how to use them: http://www.wpf-tutorial.com/misc-controls/the-windowsformshost-control/
<Window x:Class="WpfTutorialSamples.Misc_controls.WindowsFormsHostSample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
Title="WindowsFormsHostSample" Height="350" Width="450">
<Grid>
<WindowsFormsHost Name="wfhSample">
<WindowsFormsHost.Child>
<wf:WebBrowser DocumentTitleChanged="wbWinForms_DocumentTitleChanged" />
</WindowsFormsHost.Child>
</WindowsFormsHost>
</Grid>
</Window>
We have a class in our WPF project that we want to access a control that is put into a XAML file. I have put my code below and file structure to help with my question.
Folder Structure:
Navigation Directors\ FullKioskDirector.cs
MasterTemplates \ SellAllKioskMaster.xaml
Views \ Pages \ PageTemplates \ PageAttractScreen.xaml
We want 'FullKioskDirector.cs' to access the visibility of 'PageAttractScreen.xaml'. The 'SellAllKioskMaster.xaml' is referencing the 'PageAttractScreen.xaml' in its XAML.
Here is our code below.
SellAllKioskMaster.xaml
<UserControl
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:UserControls="clr-namespace:Kiosk.Views.Pages.UserControls" xmlns:PageTemplates="clr-namespace:Kiosk.Views.Pages.PageTemplates" x:Class="Kiosk.MasterTemplates.MyContainer"
mc:Ignorable="d"
d:DesignHeight="1049" d:DesignWidth="1912" Background="White">
<Grid>
<!--I need to access the visibility of these elements from the 'FullKioskDirector.cs'-->
<PageTemplates:PageAttractScreen x:Name="pageAttract" Margin="0,100"/>
<PageTemplates:PageWelcomeScreen x:Name="pageWelcome" Margin="0,100"/>
<PageTemplates:PageProductsScreen x:Name="pageProducts" Margin="0,100"/>
</Grid>
</UserControl>
FullKioskDirector.cs
using System;
using System.Windows;
using System.Windows.Controls;
using Kiosk.Common.Common.Contracts;
using Kiosk.Views.Pages.UserControls;
namespace Kiosk.Directors
{
public class FullKioskDirector : IPageNavigation
{
public FullKioskDirector()
{
/*
Want to control visibility of my controls that are placed and
x:Named in the SellAllKioskMaster.xaml
*/
}
How can I accomplish this?
It's better if you do it in an MVVM approach, rather than doing everything from code behind.
Nevertheless, wherever you are creating FullKioskDirector, just pass in pageAttract to the constructor.
Assuming you create the FullKioskDirector at UserControl's constructor
public UserControl()
{
var fullKioskDirector = new FullKioskDirector(pageAttract);
}
Then you can use it like this
public FullKioskDirector(PageAttractScreen pageAttract)
{
pageAttract.Visibility = Visibility.Collapsed;
}
I would use a Publish / Subscribe pattern.
Example: MessageBus / EventAggregator
This is my tool of choice when dealing with dependency challenges.
Essentially, you just post a message for subscribers to react to.
In this case your subscriber will then post a response in the form of a control.
You can leverage Bizmonger.Patterns to get the MessageBus.
https://msdn.microsoft.com/en-us/library/ff921122.aspx
What I am trying to do is have a webview on the blank page generated by a template for the Windows Store. Then when I click a button, it changes the page that the webview is currently on. The problem is, I do not know how to access the XAML variable from the codebehind to change it.
<Page
x:Class="App6.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App6"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<WebView x:Name="browser" Source="https://www.google.com/" HorizontalAlignment="Left" Height="224" Margin="463,237,0,0" VerticalAlignment="Top" Width="570"/>
<Button Content="Button" HorizontalAlignment="Left" Margin="224,110,0,0" VerticalAlignment="Top"/>
</Grid>
I've tried accessing the name browser in the C# code. But Visual Studio can't find it. I've looked around for a little bit but there weren't many examples that I could find that had this....
I've tried to open this (http://code.msdn.microsoft.com/windowsapps/XAML-WebView-control-sample-58ad63f7) for an example of how to use it. But this will not build, or open with Visual Studio 2013..
code behind it
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/? LinkId=234238
namespace App6
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
}
}
}
Do something like
private void Button_Click(object sender, RoutedEventArgs e)
{
browser.Navigate(new Uri(#"www.microsoft.com"));
}
Where browser is your actual control WebView.