I am in the process of learning WPF (I don't know much yet). I know how to associate a menu subitem to an event by directly editing the xaml but, I'd like to know how to do the same thing using Visual Studio.
For instance, consider the following XAML snippet:
<Menu DockPanel.Dock ="Top"
HorizontalAlignment="Left" Background="White" BorderBrush ="Black">
<MenuItem Header="_File"/>
<Separator/>
<MenuItem Header ="_Exit"
MouseEnter ="MouseEnterExitArea"
MouseLeave ="MouseLeaveArea"
Click ="FileExit_Click"/>
I'd like to be able to associate the "_Exit" menuitem with the appropriate event handlers for MouseEnter, MouseLeave and Click using Visual Studio instead of manually editing the xaml text. I was trying to accomplish this by using the "Items" collection editor but, it seems to only allow editing of the subitem's properties and not the events. Is there another editor/dialog/etc that allows editing the subitem's events ?
Thank you for your help,
John.
P.S: thank you H.B for taking the time to format the xaml properly. :-)
It does not seem to be possible to do this.
I realize this is an old thread but since I came across it, I figured someone else may benefit from a method that I found to use Visual Studio to view/edit the Event Handlers definitions of sub-MenuItems.
This method still requires that you access the XAML file but you do not have to edit it. Simply select (click) the record in the XAML file which defines the sub-MenuItem and you will be able to access the Events page in the Properties Window (F4).
If Microsoft is listening, I would say this is a design flaw (bug?) in the Collection Editor.
I'm not sure what you mean by "edit with visual studio", so I hope I'm on the right track here...
You can give the items names in xaml:
<MenuItem Name="exitMenuItem" ....>
then in the c# codebehind:
exitMenuItem.Click += (s,e) =>
{
DoIt(); // your event handling code
}
on the Properties window for that item switch to the Events tab then double click on the box next to the event you want.
in 2008 I think that there is a button that looks like a lightning bolt that you can click to get the events. It should be in the same area as the buttons to sort alphabetically or to order by category or whatever.
Related
I have this code and I would like to open a popup on a button click for users to input a date along with a date picker and then pass the date as the parameter.
<StackPanel>
<BarContainerControl>
<BarSubItem Content="List">
<BarButtonItem Content="Button" Command="{Binding cnt}"></BarButtonItem>
</BarSubItem>
</BarContainerControl>
</StackPanel>
private void HandleStuff()
{
//Does stuff here
}
I have tried multiple things like PopupBaseEdit and FlyoutControl it didn't work.
There is a bit to do here. It is not too hard if you are just using the code-behind file, and a bit more involved if you are using MVVM.
You will need to declare the Command, implement an Action method for it (along with a second bool-valued function to determine whether it is enabled), instantiate the command linking it to the action and function, and then you can bind to it in your XAML (assuming that the Command is visible in the code-behind /the ViewModel has been set as the DataContext for your Window).
Do you have more of this done than just the little snippets you provided above? If not, there is a lot to do!
The alternative, if you don't care about MVVM and you are just using code-behind, is to attach an event handler in your code-behind to some event (like the Click event?) of your BarButtonItem and launch the DatePicker from there. That's much more in the style of Windows Forms than WPF, which intentionally builds heavily on the MVVM framework.
I have a Window in XAML with some different charts. What I wanted to do is to click that chart and open another window that shows me the same chart and another elements added to the GUI, but I don´t know what I am looking to, I am not sure if MVVM would help me or shoukd I start looking for something else?
You are looking for input bindings:
<ChartPart Background="Transparent">
<ChartPart.InputBindings>
<MouseBinding Command="{Binding ShowInAntoherView}"
CommandParameter="{Binding Path}"
Gesture="LeftDoubleClick""/>
</ChartPart.InputBindings>
(Background=Transparent is important, because if the background is null, control does not pass a check if it was clicked)
This allows you invoke commands with parameters on pretty much all controls on a variety of possible user actions.
Just pass a clicked chart viewModel to a command that will navigate to it - done.
What a <ChartPart> is in your case depends on a library you use (in your case it is Livecharts), but it should not be a problem to figure out.
Somethings went wrong when I open Mainwindow.xaml.I couldn't drag, move or edit control(button, textbox,.....).
Click to see my error
This is what I try to fix it:
•I try to edit by coding, every controls in designing form still were changed when I edited by code but could not move or edit by designing
•I also try to drag some controls from toolbox to designing form and it work correctly(every controls could move or edit by designing) but controls appear before still perform.
•Try open Mainwindow.xaml by Blend Visua Studio
I'm using WPF, Design by Ribbon- Control
THANKS!
If you accidentally (or intentionally) engaged the Lock on the controls, that would prevent you from dragging them around.
Go the the Design menu in VS and then Lock then select "Unlock All" and see if that helps.
What I'm trying to do right now: Modify the Expression Blend UI / Visual Studio, to add a button on one of my dependency properties, and when I click on it, it creates a new trigger.
What is working: I created the button, it appears in the UI, that's fine.
What is not working: I cannot modify the Resource to add a trigger (if I step in, it works but it does not modify on the global resource, only on the instance I think).
I have my main project in Visual Studio, and a property with a button like this:
When the button is pressed this is what happens:
I get my Control that contains that dependency property (Ok).
I searched for the Resource file that contains the Resource I want to modify (Ok).
I update the Resource, but it does not replace the Resource on disk.
I think that it's because I only modify it on memory, so it's in the "air"
I don't know where to go now... I need help
The code behind where I modify the Resource is in an other DLL, the MyLibrary.Design.cs
I'm using Visual Studio 2010 / Blend 4 / .NET 4.0
That's one splution I can figure at the moment, but unfortunately I'm not able to test it now. Still you can check if it works for you.
You can have a class, say ResourceContainer.cs, and collect your control in it as a public static value, and let your control be a button:
public static Button MyButton;
Then you can use it in your code-behind:
If your window is MainWindow.xaml, and, say you need your particular control in a grid, then you probably have something like:
<Window x:Class=...
...(namespace stuff)...
Title="MainWindow" ...(size stuff)...>
<Grid x:Name="MyGrid">
...(your code here)...
</Grid>
...
</Window>
Then in MainWindow.xaml.cs you can use ResourceContainer.cs like this:
...
ResourceContainer rc;
...
MyGrid.Resources.Add("MyKey", rc.MyButton);
...
(when you need it)
rc.MyButton.Triggers.Add(TriggerBase item);
...
I've got a WPF application.
On the left side there is a stackpanel full of buttons.
On the right side there is an empty dockpanel.
When user clicks a button, it loads the corresponding UserControl (View) into the dockpanel:
private void btnGeneralClick(object sender, System.Windows.RoutedEventArgs e)
{
PanelMainContent.Children.Clear();
Button button = (Button)e.OriginalSource;
Type type = this.GetType();
Assembly assembly = type.Assembly;
IBaseView userControl = UserControls[button.Tag.ToString()] as IBaseView;
userControl.SetDataContext();
PanelMainContent.Children.Add(userControl as UserControl);
}
This pattern works well since each UserControl is a View which has a ViewModel class which feeds it information which it gets from the Model, so the user can click from page to page and each page can carry out isolated functionality, such as editing all customers, saving to the database, etc.
Problem:
However, now, on one of these pages I want to have a ListBox with a list of Customers in it, and each customer has an "edit" button, and when that edit button is clicked, I want to fill the DockPanel with the EditSingleCustomer UserControl and pass it the Customer that it needs to edit.
I can load the EditCustomer usercontrol, but how do I pass it the customer to edit and set up its DataContext to edit that customer?
I can't pass it in the constructor since all the UserControls are already created and exist in a Dictionary in the MainWindow.xaml.cs.
so I created a PrepareUserControl method on each UserControl and pass the Customer to it and can display it with a textbox from code behind with x:Name="..." but that is not the point, I need to DataBind an ItemsControl to a ObservableCollection to take advantage of WPF's databinding functionality of course.
so I tried to bind the ListBox ItemSource in the View to its code behind like this:
<UserControl.Resources>
<local:ManageSingleCustomer x:Key="CustomersDataProvider"/>
</UserControl.Resources>
<DockPanel>
<ListBox ItemsSource="{Binding Path=CurrentCustomersBeingEdited, Source={StaticResource CustomersDataProvider}}"
ItemTemplate="{DynamicResource allCustomersDataTemplate}"
Style="{DynamicResource allCustomersListBox}">
</ListBox>
</DockPanel>
which gets a stackoverflow error caused by an endless loop in the IntializeComponent() in that view. So I'm thinking I'm going about this in the wrong way, there must be some easier paradigm to simply pass commands from one UserControl to another UserControl in WPF (and before someone says "use WPF commanding", I already am using commanding on my UserControl that allows the user to edit all customers, which works fine, but I have to handle it in my code behind of my view (instead of in my viewmodel) since I need the parent window context to be able to load another user control when its finished saving:
<Button Style="{StaticResource formButton}"
Content="Save"
Command="local:Commands.SaveCustomer"
CommandParameter="{Binding}"/>
private void OnSave(object sender, System.Windows.Input.ExecutedRoutedEventArgs e)
{
Customer customer = e.Parameter as Customer;
Customer.Save(customer);
MainWindow parentShell = Window.GetWindow(this) as MainWindow;
Button btnCustomers = parentShell.FindName("btnCustomers") as Button;
btnCustomers.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
}
So how in WPF can I simply have a UserControl loaded in a DockPanel, inside that UserControl a button with a command on it that loads another UserControl and sends that UserControl a specific object to which it can bind its controls?
I can imagine I just do not know enough about WPF commands at this point, if anyone can point me in the right direction from here, that would be great, or if you think this "loading UserControls in a DockPanel pattern is foreign to WPF and should be avoided and replaced with another way to structure applications", that would be helpful news as well. You can download the current state of my application here to get an idea of how it is structured. Thanks.
I've just finished a LOB application using WPF where this sort of problem/pattern appeared constantly, so here's how I would have solved your problem:
1) In the DataTemplate where you create each item in the ListBox, along with it's edit button, bind the Button's tag property to the Customer object underlying that list box item.
2) Create a Click event handler for the button, and set the Button's Click event to fire the handler.
3) In the event handler, set the Content property of the UserControl.
4) Set up a DataTemplate in scope of the User Control (perhaps in the resources of it's immediate container) which describes an editor for that single customer.
Another approach that will work is to declare a Customer dependency property on your EditCustomer class, then set that property (perhaps through a XAML Trigger) when the button is clicked.
I hope this isn't too vague. If nothing else, know that the problem you're facing is very solvable in WPF.
This is where you use the Mediator pattern. There's several blog posts on this topic (for instance), and there's implementations of the pattern in some WPF frameworks (such as EventAggregator in Prism).
I don't have the time to really dig into this (it's an interesting question and I hope you get a good answer-- I can see myself running into a similar situation in the future).
Have you considered getting a little less WPF-y and falling back to firing an event on your source UserControl with an EventArgs that contains the customer, then in the event handler, firing the appropriate command on the target control?