WPF Adding textblock on an item and binding it to a textbox - c#

I have a diagram designer program and I want to add texts to shapes (these are path objects) when user right clicks to a shape and writes shape name in the property window. I add a context menu property to shapes and when user clicks to "properties" in the context menu, a new window opens that has a textbox and a button. I can add a textblock to the shapes but I cannot bind the textbox in the property window to the textblock in the shape. What I want is when user enters a text into textbox in the property window and clicks OK button, the textblock on the shape changes to the text that user entered.
Thanks.

You can bind one control to another using an ElementName binding:
<TextBlock Text="{Binding Text, ElementName=TextBoxInPropertiesWindow}" />
but that's probably not what you want in this case, because it sounds like the properties window and the text box will soon be going away and/or being reused to edit other diagram elements.
You therefore really need to be thinking in terms of binding both the text box and the text block to the underlying data model / viewmodel. In this way, the text box can update the model (which will still remain after the text box is destroyed), and the text block will then update in response to the change in the model.

Bind the selected shape's datacontext to a property on the window or controller called SelectedItem, then bind the property window's datacontext to SelectedItem.

Related

Set textbox as focused element

I am trying to set focus on a text box. I tried set focus for cursor
but all that does is put the cursor there and freezes it. You can't actually type anything still. You end up still having to click the text box to type. I did exactly as they did in that question. I read some stuff about logical focus and physical focus? Not quite sure how to go about doing what I need. Do I need to create an attached property and handle it that way? I am trying to avoid code behind at all costs.
Also, after clicking a "Submit" button I would like to have it set focus back to the text box again
Here is the code I tried, the FocusManager is on a grid that wraps the textbox:
FocusManager.FocusedElement="{Binding ElementName=FocusedTextBox}"
<TextBox Text="{Binding serialNumber}"
x:Name="FocusedTextBox">
</TextBox>
As I said, all this did is put the cursor there and you can't type anything until you again click in the text box. Any suggestions?
Thanks,
Just give the textbox a name (e.g. "theTextBox') and call theTextBox.Focus().
If you're trying to do this in MVVM with data-binding (and you should be) then you can use an attached property to bind to a property in your view model.

WPF MVVM Change button content with loading user control

I have a simple window with two buttons on bottom, when one button is clicked it change the view model of my main window, the name of the content button and the command.
I would like that when user click on the button change this with a loading user control i already have but i'm stacked, i don't know it's something i can implement with xaml or i need some code on my view model.
Any suggestions would be appreciated.
Thanks.
You will need some code on your ViewModel to swap out the Button for the Loading control. You can either do this with binding ContentControls or with a Boolean property to set one to visible and the other to hidden while the loading happens.

How design left menu panel

in windows wpf i want design a left bar menu like this:
when i click on an item, in the right the window change.
With which control can i design this?
Tab bar control?
Or a left dock panel and insert inside it some button? In this case how can I change the window on the right when click on a button?
If each time you click on a button you want to display a different view, you could define each of your views in a separate control and in your main window within a grid declare each view in a DockPanel.
Then by binding the visibility property of each DockPanel you can create the logic in order to display only what you want on each click.
I suggest using a styled ListBox and binding the Visibility property of your content to it. Even better you can also use a styled Tab control.

Open a Popup from a Scrollviewer

I'm making a WinRT application using XAML and I have a control that has a TextBox and a Popup that opens under it when the TextBox get focus.
The Width of the Popup is the same as the Width of the TextBox. This works fine but if I put my control in a ScrollViewer and zoom in the size of the popup doesn't change. I would like to achieve something similar to the standard ComboBox which changes the size of its Popup when it is nested in a ScrollViewer. What should I do?
I know that the popup must be part of the VisualTree but I'm not quite sure how to add it without changing the existing layout.
Thank you
To have the popup be parented in the TextBox you can either modify the template of the TextBox and put it in there (you could subclass TextBox to both add support for your dropdown logic and change the default template at the same time) or simply find the root Grid of the TextBox using VisualTreeHelper. By default TextBox has a Grid at its root, so you could get that and add the Popup to its Children.

WPF: Editable Radio Button

I need something like "TextBox like Label" for "RadioButton content".
I have made all database connections and bindings successfully and all my Radiobuttons are created according to my database information and no problem.
When user adds a new item to the RadioButtons, he/she would be able to change its content until he/she presses enter or focused out. RadioButton content behaves like Label now but somehow I would like to have a TextBox like behaviour to be able to edit it. After editing, its content should continue its default behaviour.
Is there any solution for this using RadioButton (Editing template) or I should think about writing my own Radiobutton with one button and one editable label?
Thanks in advance!
The RadioButton Class extends ContentControl which means, that it has a content which can be defined as you want.
So what you can do is just set a editable TextBox as Content of a Radiobutton
example:
<RadioButton>
<TextBox Text="Test" />
</RadioButton>
didnt implement the editability of the TextBox, this you have to do yourself. but the way to go is to create a usercontrol which manages the content of the related contentcontrol (radiobutton)
This is WPF, why not just compose a Radio button that contains a textbox? You can bind the texbox text to whatever property you would normally bind for the Radio Button value.

Categories