WPF datagrid not refreshing like asked - wpf

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.

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.

DevExpress GridControl ColumnAutoWidth

The question is probably silly, but I spent half of the day looking for the answer with no luck.
I have a WPF view with DevExpress GridControl included (not developed by me). The problem is that when I click "Auto fit" option - it gets too wide with a horizontal scroll. The reason is long header titles, but it's Ok if they are wrapped into 2 lines.
After some searching, I thought that what I need is ColumnAutoWidth property. The problem (here goes the silly part) is that I can't find out how to set it! Because of this I can't check if it works at least! There are no examples in documentation, and code autocomplete doesn't show it's presence anywhere. I had some assumptions but they appeared to be wrong.
Can somebody please share XAML (or at least code-behind) example about how to access this property?
you just have to remove width element from your columns, and to set the autowidth element of the tableview. the link you posted is related xtraGrid control and it is a windows form not a WPF one, you have to control the width from the TableView.AutoWidth property.
Try to read the following documentation
Your link is documentaion for WinForms grid. For Wpf GridColumn http://documentation.devexpress.com/#WPF/DevExpressXpfGridGridColumnMembersTopicAll
As I understand you question - you want to fix column width. Try this
<dxg:GridColumn Header="MyAwesomeColumn"
Width="40"
FixedWidth="True"
FieldName="MyFieldName" />

TextBox inputing data issue with MVVM Light.?

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.

Having trouble entering time values with MaskedTextbox

I'm using the MaskedTextbox for .NET 3.5 SP1, from the WPF toolkit. I've got a MaskedTextbox on a WPF page, it fills fine from a SQL Server database, but when I tried to edit the value nothing at all happens. I can select anything, but typing over it, or pressing the delete key or anything else I can thing of, does nothing to the value there. I want to use this to allow a user to enter or edit a time value, like this: 9:30 AM would appear as "09:30 AM". Here's the XAML that I've specified:
<cusControls:MaskedTextBox x:Name="mtbTime"
Mask="90:00 >LL" Margin="5,0,0,0"
Text="{Binding ElementName=ThisDateTime,Path=TimePart,Converter={StaticResource NullableTimeToUnderscoreConverter}}" />
(This is a part of a user control I'm making called "ThisDateTime".) What am I doing wrong and how do I fix it?
I haven't seen the source for the text box, but ran into something similar myself.
Could it be a problem with your partially-entered text either not matching the mask, or not matching the converter?
Is the UpdateSourceTrigger on the Binding somehow set to UpdateProperty, or is something similar happening in the code-behind?

Winforms - a strange problem a with simple binding

It's hard for me to clearly describe my problem but I'll try. I have a UserControl1 which contains UserControl2 which contains several WinForms controls (most of them DevExpress). I do simple binding to these controls to my datatable fields. So far everything works fine. When I move the focus to a record in the table (by navigating in a grid rows for example) the binding works great, the concurrenmcy manager moves the cursor and everything reflects right in the bounded controls.
The problem starts when I add new user UserControl3 above UserControl2 and make UserControl2.Visible = false. Now UserControl3 is shown and UserControl2 exists but not shown. Now when I set UserControl2.Visible = true to show it again the simple binding stops working! I navigate in the grid but either the ConcurrencyManager stops working or the simple binding becomes disconnected.
My question: Are there any known issues/ best practices with the binding & concurrency manager?
Thanks a lot,
Adi Barda
I found the answer. The guys from devexpress helped me and now I know that in order to succeed with binding one should use the BindingSource object and not use directly the dataset/datatable objects. This solved all my binding problems. I beleive the BindingSource simply working correctly with the concurrency manager and the bindingContext objects.

Resources