I'm developing a touchscreen app. I have a form with buttons with the content "new player" and then a keyboard with a textbox is shown for the user to enter his name. Now I need that when I close (or while inputting the name) the button from the parent window change the content to the name that the user typed. But I cant make a binding to the parent.. how can I do it??
Are you implementing the Model-View-ViewModel pattern in your WPF application, or using databinding at all?
You don't have to use MVVM, but you should really be using databinding in some fashion.
With databinding, you'd bind the button content to a string property of some object (a view-model in MVVM). That object needs to implement the INotifyPropertyChanged interface. You can bind the textbox on the child window to the same property of the same object.
With that in place, the button will be updated automatically when the user types in a new player name.
If you need more details, please comment and I'll be glad to elaborate.
You can try something like:
var button = this.Parent.Controls.OfType<Button>().Select(b => b.Name == "NameOfControl").First();
button.Content = name;
Edit: And Jay's idea is the right way at going at this, i was just offering an alternative way of accessing the controls from their parent using LINQ.
Related
In my MainWindow,
I have a button btnMainMenu
I have a user control NavigationWindow
from the user control, I want to change the visibility of the btnMainMenu form the MainWindow. I have tried most of the steps suggested in other posts but none of them seems to work for me.
I tried this line of code but got error.
System.Windows.Controls.Button btn = (this.Parent as MainWindow).Controls["btnMainMenu"] as Button;
I would recommend to please go through some design patterns.
This is not supposed to be a recommended way to get an object and changed the visibility.
Have a view and a view model make some dependency and change some property of view model.
Just for your learning, I can tell you one of the way but definitely would not recommend it in your product.
You can use recursion on the child controls of the parent control till you find out the button control you need.
I am not using anything other than a simple WPF application project in visual studio. I've implemented an mvvm application.
I want to display a list of content changes made by a user. I have a main window view model and it currently just builds a strings with changes. I have objects that I can reuse to display their properties (the content).
Currently, I use a MessageBoxResult to show a really long string with the changes. This is a terrible design (I know), but I couldn't really find an answer to what class a regular wpf project has that would allow me to achieve what I want.
I know there is a popup class I can use. In practice, which is better-- another view model for the dialog, or a popup?
Can anyone provide a simple example of one of the two approaches?
Thank you in advance for your response.
What I've done in the past is have a simple Border control, and inside of a TextBlock and whatever Button controls I need. I bind the TextBlock.Text to a public string property named "MessageBoxMessage" which calls OnPropertyChanged(). I bind the Command of each Button to a separate public ICommand which specifies what action to take in the view model when the button is clicked. I then bind the visibility of the Border control - which contains all of the other controls I mentioned - to a Visibility property.
When I want to show a dialog, I set the MessageBoxMessage to the message I want to show, makes sure the commands are set properly, and then set the Visibility on the Border to Visibility.Visible. This shows the box (border), message, and buttons.
You can even implement a semi-transparent rectangle underneath the border (over the rest of the form) that you set to visible at the same time. This will give you the nice "form dimmed" effect and also block the normal form controls from being clicked. A general note - for this to work, these controls need to be at the very bottom of your XAML as the z-index among controls at the same level is inferred from their placement in the XAML - lower in the code is top level on the form.
Let me know if you have any questions about implementing this if it sounds like what you are looking for.
In my winform app, I'm using elementhost to display the xaml. The xaml uses properties from a viewmodel. Unfortunately, when I click on the save button, (this button is a winform button), I'm not able to get any data I typed in the xaml. Can anyone advise?
this should do it just in case someone may come across this issue.
CarView car = (CarView) CarHost.Child;
CarViewModel cvm = (CarViewModel) car.DataContext;
Hi, Please consider the custom wp7 message box above. I am looking to see what is the cleanest way to bind a views textbox to a property on the view model only after a button is clicked. The only way I can work out is to have two properties and use a command on the button to assign the value of the first prop to the main prop. The main prop should only receive the value from the textbox only if the user clicks the tick button and not if they cancel (by pressing the back button).
I am using MVVM Light.
What about catching the click event and sending the changed text from View to ViewModel via Messenger. On ViewModel the property would be only a getter.
I think that the way you do it is ok.
Have a temporary binded property and main property synced with the temporary one only when messagebox is accepted.
I m working in a WPF MVVM Project and I am using one Wpf Window. In this window i have a space for one usercontrol and two buttons next and back. When i click the next button I want usercontrol1 to be replaced with usercontrol2. etc.
Sounds like you're trying to create a wizard-style user interface.
This Code Project article may help. And right here is a good place for getting started too.
You can have Wizard user control which is binded to WizardModel, In the Wizard control put ContentPresenter control, and bind it to WizardModel's WizardPage property. And by changing that WizardPage property, you can change wizard page from model.
Hope this helps, here used the same mechanism for changing views