TextBox inputing data issue with MVVM Light.? - wpf

I am working on WPF MvvmLight application and once I click on first page it navigates to second page. The second page has a textbox on which the focus is set. On this textbox, I am not able to put any data or any character. But I can do copy and paste. What might be the reason for this strange behavior.
<TextBox VerticalAlignment="Center" x:Name="txtsearchYouTube" Height="25"
Margin="100,0,100,0" Canvas.Top="275" Width="500"
Loaded="txtsearchYouTube_Loaded" Canvas.Left="50"
Text="{Binding SeachKeyWord,Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}"/>

1) The Binding is not the issue, in fact even if it is wrong, you will still be able to write in that TextBox, it just won't be put in SearchKeyWord. By the way, you mispelled it (you forgot an R in seaRch).
2) If you're really only setting the focus on the loaded event, and nothing else, then it's not the problem either.
3) MVVM Light is just a conveniency framework, it does not alter WPF for you as it just provides tools, it definitely doesn't mess with TextBoxes.
That said, here is what I would check in your situation:
If SearchKeyWord is a dependency property look where it is declared to see if there is a callback/coercevalue/validatevalue declared. Since the binding is two-way, it could very well always invalidate the values recieved (preventing you from actually writing anything in there)
Check all the way up your visual tree, for any implicit styles for the TextBox type. They could have triggers that mess up with your data.
Hope this helps,
Bab.

i dont know whats wrong with your code. but you can help your self. so just remove your binding and loaded event and check if you can put any data in your textbox.
if yes pls post all code from your event and viewmodel otherwise its hard to help.
you wrote you can copy and paste? what does it mean? your textbox show the text you paste in? or your viewmodel got the pasted value?
EDIT: you can also use Snoop to check your textbox binding and properties at runtime.

Related

How to use a control equivalent to the WinForms DataGridView in WPF

I've been creating an inspection form using WPF and I need a place where users can type an unknown amount of comments (hence why I'm not using textboxes). In my WinForms version of this application, I used a DataGridView and I could enter in as much information as I wanted to. I'm looking to do the same with a DataGrid or an equivalent control in WPF.
WinForms Example
I need to be able to do the same thing in WPF but I can't seem to add any rows in the DataGrid. On top of that, when I try to check CanUserAddRows it unchecks it immediatly.
So I checked out Vincent Sigal's blog post about this issue. He mentions something interesting:
... but beware of CanUserAddRows and CanUserDeleteRows as they can appear a little magical. Their values are coerced based on other properties such as DataGrid.IsReadOnly, DataGrid.IsEnabled, IEditableCollectionView.CanAddNew, and IEditableCollectionView.CanRemove. So this is another thing to watch out for when editing. If you run into a situation where you set CanUserAddRows or CanUserDeleteRows to true but it is changed to false automatically, check that the conditions below are met.
I verified this and my DataGrid is not read-only and it is enabled. Although, I have no idea where to find the IEditableCollectionView.CanAddNew and IEditableCollectionView.CanRemove ...
I don't think my situation should require a binding event on the DataGrid since the user is supposed to enter his comments directly into the DataGrid ... Is what I'm trying to do even possible? Perhaps I should use a different control?
I have to admit that I stopped reading through your question after the first paragraph, so please forgive me if I have understood you wrong... but if you just want to enter multi line text into a TextBox in WPF, you can do it by setting a couple of properties on it:
<TextBox TextWrapping="Wrap" AcceptsReturn="True" />
For a DataGrid, you can set these properties in the DataGridTextColumn.ElementStyle and/or DataGridTextColumn.EditingElementStyle as the WPF DataGridTextColumn multi-line input post shows quite nicely.
Please let me know if I did misunderstand you.
UPDATE >>>
Ok, so I came back to read the rest of your question... answering without reading the question can be risky business on this site. It's just as well that I did too, as I see you also want to know how to use the DataGrid.
I have to start by saying... take a deep breath... WPF is very different to WinForms... very different. In WPF we manipulate data rather than UI objects, so to add a new row actually means adding a new item to a collection. You can find a complete working example on the DataGrid Class page on MSDN.
Please also view the WPF DataGrid Control page on WPF Tutorial.NET for more examples. WPF has a lot to take in for new comers and can be quite bewildering, but it's well worth the trouble when you get into it.

WPF datagrid not refreshing like asked

I have a weird situation in one of my program when a datagrid's content is not update when I call the grid.items.refresh command.
I do know that the source of the grid is updated as it should and should I quit the windows and reopen it, which has the effect of giving the itemssource back to the datagrid, the grid is now OK.
Are there any known bug with the items.refresh command of the datagrid or is there something I am not doing right ?
Thanks,
Edit : Forgot to mention, this only seems to happen on my client computer. As usual, it's working here, but I did see the problem myself. I just can't explain it.
Some suggestions:
On your DataGrid column definitions, you can add UpdateSourceTrigger=PropertyChanged to your Column Bindings.
ie: Binding="{Binding Path=Product.ProductCode, UpdateSourceTrigger=PropertyChanged}"
If that does not help, consider changing your List to an ObservableCollection<OfSomething>. Then WPF's data binding will automatically react to changes within your source.

Specified element is already the logical child of another element

This question is related to following thread.
Prism RegionAdapter - Removing then Adding View
My post to this thread was deleted my admin, saying it was answered and I need to start new thread. Please let me know if any of you have solved this issue.
My deleted post was...
How did you manage to get rid of "Specified element is already the logical child of another element. Disconnect it first." error with closing event. I tried the same thing, on closing I remove the dockablecontent from the documentpane. But it doesn't help I keep getting this error. Once I remove the view, and then later try to add it again the same way you are adding, I get this error.
Please help me, I am not sure what am I missing. I have wasted so much of time trying to get around it but no luck yet.
Here is the code I am using:
<ad:DockableContent ...
x:Name="viewRoot"
IsCloseable="True" HideOnClose="False"
Title="{Binding Title}">
<ContentControl Grid.Row="1" Content="{Binding View}"/>
</Grid>
</ad:DockableContent>
It gets used inside this:
<ad:DockingManager x:Name="DockingManagerControl" Grid.Column="1">
<ad:ResizingPanel x:Name="MainResizingPanel" ResizeWidth="*" ResizeHeight="*" Orientation="Horizontal">
<ad:DockablePane ad:ResizingPanel.ResizeWidth="0.25*" prism:RegionManager.RegionName="ProjectBrowserRegion"/>
<ad:ResizingPanel x:Name="PespectiveResizingPanel" ResizeWidth="0.75*" ResizeHeight="*" Orientation="Horizontal">
<ad:DocumentPane x:Name="DockablePaneControl"/>
</ad:ResizingPanel>
</ad:ResizingPanel>
</ad:DockingManager>
View content bindings are UI element which comes from separate dlls, So I don't have them as View/ViewModel pair. So I can't use ContentTemplate+DataContext way here. Things load nicely, but use can select/deselect these views. So if a view has been opens the view selection dialog again, then I clear all views (I remove it from DockablePaneControl Items.Clear()), and add the selected views again (DockablePaneControl Items.Add()), then I get this error.
Usually this occurs because you are trying to assign the same control to two different parents.
I often see this issue with Styles, where a non-template property that contains Controls is set in a Style, such as ContentControl.Content, or DataGridColumn.Header
For example, if your style sets ContentControl.Content, and you add two ContentControls to your Window, you'll encounter this error because the style is trying to assign the same controls inside the Content section of both ContentControls. The fix is to set a template style, such as ContentControl.ContentTemplate instead of ContentControl.Content. I like to say this is like giving multiple people the same cookie to it - it just doesn't work. You have to give them each the cookie cutter (template) instead so they can make their own.
I actually see questions about this error so much that I wrote a blog post about it

How to create databinding over two xaml files?

I am trying to come to a working understanding of how databinding works, but even after several tutorials I only have a basic understanding of how databinding works. Thus this question might seem fundamental to those more familiar with silverlight. Even if it is trivial, please point me to some tutorial that deals with this problem. All that I could find simply solved this via adding the data binding on a parent page.xaml (that i must not use in my case).
For the sake of this example let us assume, that we have 5 files:
starter.cs
button1.xaml + codeBehind
button2.xaml + codeBehind
The two buttons are generated in code in the starter(.cs) file, and then added to some MapLayer
button1 my_button1 = new button1();
button2 my_button1 = new button2();
someLayer.Children.Add(my_button1);
someLayer.Children.Add(my_button2);
My aim is to connect the two buttons, so that they always display the same "text" (i.e. my_button1.content==my_button2.content = true;). Thus when something changes my_button1.content this change should be propagated to the other button (two way binding).
At the moment my button1.xaml looks like this:
<Grid x:Name="LayoutRoot">
<Button x:Name="x_button1" Margin="0,0,0,0" Content="{Binding ElementName=x_button2, Path=Content}" ClickMode="Press" Click="button1_Click"/>
</Grid>
But everthing that i get out of that is a button with no content at all, it is just blank as the binding silently fails.
How could I create the databinding in the context I described? Preferably in code and not XAML ;)
Thanks in advance
The chunk of documentation you need to read is this: XAML Namescopes
Your button1 xaml has a binding looking for an element with the name "x_button2". However in a real application there can be many controls which in turn have nested controls. All of these controls have all manner of UI elements some of which may have names.
It would be impossible to get anything done if all names throughout the entire application had be unique. Yet that would need to be true if it were for your button1 to be able to hunt down the existence of another control somewhere in the visual tree outside of that which it actually knows (its own xaml).
Hence each loaded Xaml document exists in its own "namescope" and the search for other elements with other names is limited to that "namescope".
The are various solutions to this problem depending on what you real requirements are as opposed to the simplified problem in your question.
Typically you give each of your controls a DependencyProperty to which the inner button Content property binds. In "MapLayer" as call it, could then bind the propert on one of your button controls to the other.

Cannot see named Silverlight control in code

In my first few hours with Silverlight 3, as an avid WPF user, I am greatly disappointed at the many things it doesn't support. This seems like an odd issue to me and it's so generic that I cannot find anything online about it.
I have the following XAML:
<controls:TabControl x:Name="workspacesTabControl" Grid.Row="1"
Background="AntiqueWhite" ItemsSource="{Binding Workspaces, ElementName=_root}"/>
However, I cannot see the workspacesTabControl in code-behind. I thought maybe IntelliSense is just being mean and tried to go ahead and compile it anyway, but got an error:
Error 1 The name 'workspacesTabControl' does not exist in the current context
How do I access controls in code-behind?
EDIT: I realized I've pasted the wrong error - I have two controls inside the UserControl called workspacesTabControl and menuStrip. I cannot get to either one of them by their name in the code-behind.
Just in case, here is the XAML for the menuStrip:
<controls:TreeView Grid.ColumnSpan="2" Height="100" x:Name="menuStrip"
ItemContainerStyle="{StaticResource MenuStripStyle}"
ItemsSource="{Binding Menu, ElementName=_root}"/>
EDIT AGAIN:
I'm not sure if this is helpful, but I've taken a look at the InitializeComponent() code and here's what I saw:
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
public void InitializeComponent() {
if (_contentLoaded) {
return;
}
_contentLoaded = true;
System.Windows.Application.LoadComponent(this, new System.Uri("/SapphireApplication;component/SapphireMain.xaml", System.UriKind.Relative));
}
It seems that it simply loads the XAML when it runs (not before or during compilation) so the menuStrip and workspacesTabControl names don't actually get registered anywhere (as they usually are in WPF/win Forms). Could that attribute be a problem? And where do I get rid of this requirement for all the future UserControls I make?
Check the properties in VS for the xaml file itself... make sure the Build Action is set to Page.
As ridiculous as it may sound, I have resorted to using FindName() method to access named items in code-behind:
this.FindName("workspacesTabControl") as TabControl
I realize that this is a ridiculous way but I am forced to use this for now. Please let me know if someone else has encountered this problem and have come up with a better solution!
When you first create a control, Visual Studio does not pick it up with intellisense. However, after you try to build the project, it should become availble. You can also just type the name in without intellisense and then build it. Haven't verified this, but I heard this was on the list of things to fix in SL4.
That being said, if you name a control inside of a datatemplate, that control is not directly accessible in code-behind. This is the same for WPF, though.
You should be able to see it in the codebehind, that part works the same as WPF, maybe if you fix the problem with the menuStrip, then visual studio will be able to build the xaml paty of the page and ull be able to access the tabcontrol
I've seen the same problem in my Silverlight development. Specific to my problem my named controls were nested inside other controls (i.e. a datagrid) and I was unable to access them in my code behind. Any named controls at the same nesting level or above the previously mentioned datagrid worked fine but anything inside it was lost into the abyss.
As already mentioned, it should just appear in Intellisense, however the fact that you're getting an error related to something else, i.e. "menuStrip" is probably interfering with Intellisense. Resolve that error and you'l probably find that you can access the "workspacesTabControl" control.
Are you possibly using some sample code or something where they've named a control "menuStrip" and you've renamed it?
Good luck
Check that you don't have any controls using the same class name as a namespace name. For example:
namespace Solution.ProjectName.workspacesTabControl
{
public class workspacesTabControl
{
...
}
}
This will also give you this error.
Good luck,
Mark

Resources