I'm having a Blank solution with multiple projects. I want the Wpfloginwindow to be linked with the other WPF projects (WpfMembers). What I mean is when I click on the connect button in the MainWindow.xaml of the Wpfloginwindow, I want the Mainwindow.xaml of WpfMembers to be shown. How can I do that? I only know how to do that when I'm having multiple windows in one project. But here is different I want to call the WPF window of another project but in the same solution (SlnProject).
An WPF window is a normal class that inherith from Window class. You can just call your member window from your login window, referencing the namespace from your member window:
Main window Login XAML:
<Button Click="Button_Click">Connect</Button>
code behind:
private void Button_Click(object sender, RoutedEventArgs e)
{
var mainWindowMember = new WpfMembers.MainWindow();
mainWindowMember.Show();
}
This is the simplest way to implement it. To improve your project architeture and decouple your business logical from UI, maybe you want to implement MVVM pattern:
https://learn.microsoft.com/en-us/archive/msdn-magazine/2009/february/patterns-wpf-apps-with-the-model-view-viewmodel-design-pattern
Related
Im using WPF to make a simple budget app for myself as practice and I can't figure out how to change which window opens first. I mean when you start the program , right now it opens the MainWindow, but I want it to open another window. I have tried this in my app.xaml.cs file:
public partial class App : Application
{
void App_Startup(object sender , StartupEventArgs e)
{
GetNameWindow getNameWindow = new GetNameWindow();
getNameWindow.Show();
}
}
which I read was a way to do it but it doesn't work for me. I'm using c# and visual studio 2017. Thanks!
To change the startup window, open App.xaml and replace "MainWindow.xaml" with your window:
StartupUri="GetNameWindow.xaml">
For what you were trying to do, you would need to remove StartupUri="MainWindow.xaml" and instead use Startup="App_Startup" and then it would call your event Handler at startup.
I apologize if the question title isn't really specific, I'm not exactly sure how to condense the problem I'm having down to a few words. But to simplifiy the problem I'm having, here is my issue:
I'm creating a tool using WPF that consists of a TextBox that will contain a path to a directory and a Button that will allow you to Browse to a certain directory. Now, when I select the Browse button, it pops up a dialog, allows the user to select a directory and then I have some methods that will disable some buttons and updates some Brushes on the screen if the path doesn't meet a certain set of criteria. No problems there, got that working.
My problem is the TextBox that this Browse button correlates with. This TextBox is using a binding as such:
In my MainWindow.xaml (Yes, this is the simplified, focused version):
<Window>
<TextBox Text="{Binding Directory}" TextChanged="Directory_TextChanged" />
<Button Content="Browse..." Click="Browse_Click"/>
</Window>
In my code MainWindow.xaml.cs file:
public partial class MainWindow: Window
{
private ViewModel myViewModel;
public MainWindow()
{
myViewModel = new ViewModel();
this.DataContext = myViewModel;
}
private void Browse_Click(object sender, RoutedEventArgs e)
{
// Dialog stuff that's working
viewModel.Directory = dialog.SelectedPath;
}
private void InstallDir_TextChanged(object sender, TextChangedEventArgs e)
{
ValidatePath(); /* Disables/enables buttons and updates brushes based on validation. Also working */
}
private void ValidatePath() {/* */}
}
Like I mentioned earlier, the browse button works fine. I'm trying to figure out however, how I can get this to work if I type a directory alongside it. Because if I type something in the textbox, that would mean that inside of the InstallDir_TextChanged() function I would have to set viewModel.Directory, but since I have the INotifyPropertyChanged attached to this ViewModel, this function would get called recursively.
I tried doing the validation stuff within the viewmodel, but I couldn't figure out how to update the brushes/buttons in MainWindow if I did this. (Still relatively new to C# so I haven't learned the ins and outs yet. This is the first WPF tool I've been making from scratch, so just a disclaimer).
Would anyone have any ideas (or logic) I can approach to try and accomplish this? If there's any further clarification needed, that's not an issue. I don't need an exact definitive answer. Maybe some advice that could point me in the correct direction would definitely suffice. I don't have a problem trying to figure stuff out.
Not terribly familiar with WPF and C# so if this is blatantly wrong, please correct me. Working in VSExpress2015 .NET Framework 4.5. I'm heavily simplifying my code below, so know that namespace/library references are there.
Say I have a window with a Button and ContentControl inside:
<Window x:Class="Project.MainWindow">
<Grid>
<Button Name="Submit_Btn" Click="Submit_Btn_Click">
<ContentControl Name="MainContentControl">
</Grid>
</Window>
I also have several user control files in my project with XAML that looks something like this:
<UserControl x:Class="Project.UserControl1">
<Grid>
<TextBox Name="TxtBox1">
</Grid>
</Usercontrol>
I have code in the backend of my MainWindow to dynamically load the appropriate UserControl into the "MainContentControl" object. However, I want to reference the objects inside of the currently loaded UserControl from the MainWindow's Submit_Btn_Click function. For example, in MainWindow.cs, do something like this:
private void Submit_Btn_Click(object sender, RoutedEventArgs e)
{
if(MainContentControl is currently loaded with UserControl1)
Do_Something_Function(MainContentControl.TxtBox1.Text);
}
The main problem here is I don't know how to call the TextBox1 element from within the parent MainWindow's scope. I'm also not sure how to validate the if condition (confirming the control is currently loaded). Does anyone know of a way to think about this differently or even directly reference the object (despite that probably not being a great idea)?
--
I'm not using/familiar with MVVM at all (yet), and I'm not particularly concerned with optimal performance as this is a one off temporary project that will soon die and be re-worked. I've read ways how to access the data in a parent window from a child, but I didn't find scenarios that really matched up with this.
Once again, I'm still familiarizing myself with C#, WPF and general coding practices (it's been a couple years), so if using a ContentControl or UserControl here isn't optimal, (or mixing the two doesn't make sense) that information would be greatly appreciated; however, in this scenario, I'm more concerned with just getting this working until I can learn more proper techniques later.
Instead of trying to access the TextBox inside the UserControl, you can expose properties and methods on the UserControl itself to interact with what's inside. In your case, you could add a property that returns the current value of TextBox.Text. You can also add dependency properties to facilitate binding.
You can use the LogicalTreeHelper to search by name.
So for your example to access TextBox1
var txtBox = LogicalTreeHelper.FindLogicalNode(MainContentControl, "TextBox1") as TextBox;
Do_Something_Function(txtBox?.Text);
It definitely looks like the design should be improved, but just to get this working you can do the following:
private void Submit_Btn_Click(object sender, RoutedEventArgs e)
{
var controlAsUserControl1 = MainContentControl.Content as UserControl1;
if (controlAsUserControl1 != null)
{
Debug.WriteLine(controlAsUserControl1.TxtBox1.Text);
}
}
Cheers all,
I'm searching to use the devexpress tool in my visual c# project (vs2013). I'm also using caliburn.micro and i've inserted the Caliburn.Micro.DevExpress reference. So, this is my environment.
The problem: I want create a container, where you can open some predefined tabs. You can navigate in the open tabs, close them and open others.
I'm in stuck with the integration of devexpress and caliburn. Without caliburn and mostly without MVVM pattern, it's easy. But how can I do this using my viewModel?
The result I want is like the example "Simple MDI" in [the official caliburn.micro documentation1]. Instead of the button "open tab", I've my menu in the upper side and, depending on which button is clicked, I want open the relative dockpanel/tab.
Now, in the xaml file, my container is a devexpress object, the "DocumentGroup". My goal is to add dynamically the DocumentPanel(s), like written above.
Is it clear the problem? Some ideas for solution?
UPDATE: in my viewModel I have:
namespace **.ViewModels
{
class MainWindowViewModel : Conductor<IScreen>.Collection.OneActive
{
public void addT() {
Debug.WriteLine("start");
ActivateItem(new ucImpiantiViewModel());
Debug.WriteLine("end");
}
}
}
And in the xaml I have:
...
<dxd:DocumentGroup x:Name="Items" ItemHeight="3*" SelectedTabIndex="0">
...
I thought this way was good, but still nothing.. the good new is only that the console.writeline are working!
App I am trying to create in WPF/C# has quite a few buttons in a layout with a "TV screen" type panel above (its actually an FMS emulator for commercial aircraft). Many of the buttons change the layout, which are numerous TEXTBOXs on the tv screen. My question is: is there a provision to encapsulate the layouts in different classes/files and load them into the "tv screen" at the selection of the various buttons? In other words, user hits the Flight Plan button and the layout of the 355x355 box (screen) above loads the XAML "flight_plan" layout/file/class. Each layout has different TEXTBOX sizes & locations and there are in excess of 30 different "pages", which would make encapsulating them desirable.
I am very new to WPF and c#, but have written win apps in c++ all the way back to Turbo C & OWL. I also may be trying to do something that isn't possible due to working lately in Android/Java and am confusing capabilities.
Thanks in advance.
Edit
Thanks to #adityaswami89 and everyone else who got me on the right track, I have found the solution. I added the pages via a new "WPF Page" in VS2012. Then changed the "screen" to a navigation frame and it was truly simple from there. Below is the simple project I created to test it.
public partial class MainWindow : Window
{
NavRad navrad = new NavRad();
FPlan fplan = new FPlan();
public MainWindow() {..}
private void Frame_Navigated_1(object sender, NavigationEventArgs e) {..}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
Screen_Frame.Navigate(fplan);
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
Screen_Frame.Navigate(navrad);
}
You can also use the concept of Frames for the intended functionality , if that can be an option you are looking.
You can refer the below link for the same.
http://msdn.microsoft.com/en-us/library/ms750478.aspx#Frame_in_Standalone_Applications
You can abstract the different UI Layout Sets within different User Controls and load them according your UI logic. One way to do this is using an MVVM framework, for example, Caliburn Micro makes this a pretty simple task as doing:
ActivateItem(UILayoutViewModel);
And this call can be called from any method.
See more of Caliburn Screens and Composition at official source.