I have got the following window which contains my user controls in the upper area (depending on the state of the process) and a button. After click at the button one user control will be hidden and another will be shown. By the change from the first to the second control the window is frozen until the second control is finished with the tasks.
The second control contains a ListView which logs the steps. I want to see this logging directly during the process. But I can only see it when all is finish. How is it possible to refresh the window before the second control starts with it work?
A code line like
Me.UpdateLayout()
doesn't work ...
The whole program shalls be an update tool with three screens (1. settings, 2. logging of the update process, 3. finish dialog). Therefore I need to know the single steps of the logging directly after done.
My current XAML is the following:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:SiS.Controls="clr-namespace:SiSConverter"
Title="Konvertierung von SiS-Anwendungen"
Height="400" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="50px" />
</Grid.RowDefinitions>
<SiS.Controls:Settings x:Name="ucSettings" />
<SiS.Controls:Upgrade x:Name="ucUpgrade" />
<SiS.Controls:Finish x:Name="ucFinish" />
<Button Name="btnContinue" Width="100px" Height="30px" Grid.Row="1" Margin="0,0,10,0" HorizontalAlignment="Right" />
</Grid>
</Window>
And the corresponding code behind:
Class MainWindow
Private _Step As Integer = -1
Private Property [Step] As Integer
Get
Return _Step
End Get
Set(value As Integer)
_Step = value
Me.ucSettings.Visibility = Visibility.Collapsed
Me.ucUpgrade.Visibility = Visibility.Collapsed
Me.ucFinish.Visibility = Visibility.Collapsed
Me.btnContinue.Content = "Weiter"
Select Case _Step
Case 0
Me.ucSettings.Visibility = Visibility.Visible
Case 1
Me.ucUpgrade.Visibility = Visibility.Visible
Case 2
Me.ucFinish.Visibility = Visibility.Visible
Me.btnContinue.Content = "Beenden"
Case Else
End Select
Me.UpdateLayout() 'doesn't work
End Set
End Property
Private Sub MainWindow_Initialized(sender As Object, e As EventArgs) Handles Me.Initialized
Me.Step = 0
For Each Item As System.IO.FileInfo In New System.IO.DirectoryInfo("Converters").GetFiles()
Dim oConverter As ISiSConverter = System.Reflection.Assembly.LoadFrom(Item.FullName).CreateInstance("Upgrade.Main", True)
Me.ucSettings.Converters.Add(oConverter)
Next
End Sub
Private Sub btnContinue_Click(sender As Object, e As RoutedEventArgs) Handles btnContinue.Click
Select Case Me.Step
Case 0 'Einstellungen
Me.Step += 1
Me.btnContinue.IsEnabled = False
Me.ucSettings.Converters.FindAll(Function(item) item.DoUpgrade).ForEach(Sub(item) item.Upgrade())
Me.btnContinue.IsEnabled = True
Case 1 'Upgrade
Me.Step += 1
Case 2 'Abschluss
Me.Close()
Case Else
End Select
End Sub
End Class
Thanks for any response.
Edit:
The program consists of three steps. In the first step will be configured what is to do.
With click on the bottom right button shall be done something for each marked item (green button, red will be ignored) which is shown by the list of the second step.
If step 2 is done the finish Screen is shown and the program can be closed (this screen is still to design).
In WinForms and ASP.NET I show and hide user controls also in the way I did this time (maybe also this wasn't before a good implementation). It would be nice if there is in WPF a better possibility.
Your problem here is your UI thread which is the only one that can update the elements shown by your View / Window is too busy running your
Me.ucSettings.Converters.FindAll(Function(item) item.DoUpgrade).ForEach(Sub(item) item.Upgrade())
^^ function and hence cannot update the view / render elements until it's done.
A "cheat" kind of to explicitly show this (Just use this as information, don't use it in your code)
Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Background,
Sub()
Me.ucSettings.Converters.FindAll(Function(item) item.DoUpgrade).ForEach(Sub(item) item.Upgrade())
End Sub)
^^ That will show your new UserControl when the button is clicked. You still however have the issue of un-responsive UI.
You need to look at offloading work from the main thread by using things like BackgroundWorkers for what your doing from code-behind to keep the UI responsive and then whenever you need to update a UI control Notify the UI thread accordingly.
Related
I have a need to detect a dirty page when the user tries to move to a new tab and give the user the option to cancel the move off the current tab. I can get this to work when I don't need to ask the user, but showing the messagebox is breaking the functionality. I have provided a simple app that exhibits the problem. Sorry it's in VB, but that's what we use here.
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ChangeTab"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<TabControl SelectedIndex="{Binding SelectedIndex}" Initialized="TabControl_Initialized" IsSynchronizedWithCurrentItem="True">
<TabItem Header="Tab 1">
<TextBlock Text="Content 1"/>
</TabItem>
<TabItem Header="Tab 2">
<StackPanel>
<TextBlock Text="Content 2"/>
<CheckBox Content="Locked" IsChecked="{Binding IsChecked}"/>
</StackPanel>
</TabItem>
</TabControl>
</Window>
Imports System.ComponentModel
Imports System.Windows.Threading
Class MainWindow
Implements INotifyPropertyChanged
Dim tc As TabControl
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Public Function SetProperty(Of T)(ByRef storage As T, value As T, PropertyName As String) As Boolean
If Object.Equals(storage, value) Then Return False
storage = value
NotifyPropertyChanged(PropertyName)
Return True
End Function
Public Sub NotifyPropertyChanged(ByVal propertyName As String)
RaiseEvent PropertyChanged(Me, New System.ComponentModel.PropertyChangedEventArgs(propertyName))
End Sub
Private _IsChecked As Boolean
Public Property IsChecked As Boolean
Get
Return _IsChecked
End Get
Set(value As Boolean)
SetProperty(_IsChecked, value, "IsChecked")
End Set
End Property
Private _SelectedIndex As Integer
Public Property SelectedIndex As Integer
Get
Return _SelectedIndex
End Get
Set(value As Integer)
SetProperty(_SelectedIndex, value, "SelectedIndex")
End Set
End Property
Private Sub TabControl_Initialized(sender As Object, e As EventArgs)
tc = DirectCast(sender, TabControl)
AddHandler tc.Items.CurrentChanging, AddressOf Items_CurrentChanging
End Sub
Private Sub Items_CurrentChanging(sender As Object, e As CurrentChangingEventArgs)
Dim Result As MessageBoxResult
If SelectedIndex = 0 And IsChecked Then
Result = MessageBox.Show("Do you want to leave this tab?", "Leave", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No, MessageBoxOptions.ServiceNotification)
If Result = MessageBoxResult.No Then
e.Cancel = True
tc.SelectedItem = DirectCast(sender, ICollectionView).CurrentItem
End If
End If
End Sub
End Class
If you run this app and follow these steps you will find the tab control stops responding to tab changes.
1. Click TAB 2 (we move to TAB 2)
2. Check the Locked checkbox
3. Click TAB 1
4. Respond NO to the popup (we stay on TAB 2)
5. Click TAB 1 again
6. Response YES to the popup (we return to TAB 1)
7. Click TAB 2 again (we move to TAB 2)
8. Click on TAB 1
9. Response NO to the popup (we stay on TAB 2)
10. Click on TAB 1 again -- nothing happens!
NB: If you clear and check the Locked checkbox then functionality returns.
If you put a breakpoint in Items_CurrentChanging everything works correctly. Surely this is a threading issue - can anyone tell me what is wrong and, more importantly, how to fix it?
Thanks
It looks like the Items_CurrentChanging event is not being raised because the tab I'm clicking on still has focus (even though it is not the current item). I don't know why we have to go through two iterations to see the problem. The solution is to add a line of code after we set SelectedIndex. This sets focus back to the tab we didn't move away from.
Dispatcher.BeginInvoke(Sub() DirectCast(tc.SelectedItem, UIElement).Focus())
If anyone can shine a light on what's going on here, I'd be very grateful.
I'm trying to write a function in VB.Net WPF application for retrieving document’s height in MS WebBrowser control. I can get this value only after some time, when page has been rendered. So, I tried:
Private Function GetHeight(ByVal htmlstring As String) As Integer
Dim wb As New WebBrowser 'Declare WebBrowser
wb.Width = 940 'set Width
wb.NavigateToString(htmlstring) 'Navigate to content
Do Until wb.IsLoaded 'Wait until page is rendered
Loop
'Get DOM Document
Dim doc As mshtml.HTMLDocument = wb.Document
'Get sought value
Dim RetVal As Integer = CInt(doc.body.getAttribute("scrollHeight").ToString)
doc = Nothing : wb.Dispose() : wb = Nothing 'Free variables
Return RetVal 'Return value
End Function
But calling such function causes application to freeze. What should I do? Do I need to implement Async and Await keywords, as well as Threading.Tasks and how to achieve this?
According to MSDN, IsLoaded is a framework element property that indicates whether or not the control has been loaded for presentation, not whether the WebBrowser control's web page has been loaded.
While I question why you would instantiate a browser and have it navigate to a page in a method called "GetHeight"... what I think you want to do here is subscribe to the LoadCompleted event: https://msdn.microsoft.com/en-us/library/system.windows.controls.webbrowser.loadcompleted(v=vs.110).aspx
Perhaps you can construct your webbrowser and have it navigate in a different method, and subscribe to the LoadCompleted event with your GetHeight method?
Edit I neglected to mention that because you declared your browser control outside of xaml, the control will never be loaded (and thus, navgiation won't work) because it's not in the visual tree of the WPF application. You'll have to either use a webbrowser already declared in xaml (which I would recommend), or add it programmatically with something like this:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<StackPanel x:Name="MainPanel">
<Button Click="Button_Click"> Clicky</Button>
</StackPanel>
Class MainWindow
Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
Dim wb As New WebBrowser 'Declare WebBrowser
wb.Width = 940 'set Width
Me.MainPanel.Children.Add(wb)
wb.NavigateToString("www.stackoverflow.com")
AddHandler wb.LoadCompleted, Sub(s, ee) DoSomething()
End Sub
Private Sub DoSomething()
MessageBox.Show("blah")
End Sub
End Class
[disclaimer: I am new to Visual Basic.]
In a WPF, I have a TabControl containing 2 TabItems:
The first TabItem contains a bunch of URLs.
The second TabItem consists of a DockPanel that contains a cefSharp webView (chromium embedded for .net)
When I click on a url in tab1 it loads a page in the browser contained in tab2... But, it only works if I have initialized the browser first by clicking on tab2.
After doing some searching, it looks like vb.net doesn't initialize the content in a TabItem until it becomes visible. (right?)
So, my question is, how can I force a non-selected tab to initialize its content on load, in the background? (ie. so I don't have to click on the tab or switch to it first)
EDIT:
As requested, here is the relevant code:
The relevant XAML consists of a single DockPanel named "mainBox"
<DockPanel Name="mainBox" Width="Auto" Height="Auto" Background="#afe0ff" />
And here is my "code behind" vb script:
Class MainWindow : Implements ILifeSpanHandler, IRequestHandler
Shared web_view1 As CefSharp.Wpf.WebView
Shared web_view2 As CefSharp.Wpf.WebView
Public Sub init(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Loaded
'This is in a DockPanel created on the xaml named mainBox
' set up tabControl:
Dim browserTabs As New TabControl()
browserTabs.BorderThickness = Nothing
Dim tab1 As New TabItem()
tab1.Header = "My Tab"
Dim tab2 As New TabItem()
tab2.Header = "Browser"
Dim tab1Content As New DockPanel()
Dim tab2Content As New DockPanel()
tab1.Content = tab1Content
tab2.Content = tab2Content
browserTabs.Items.Add(tab1)
browserTabs.Items.Add(tab2)
mainBox.Children.Add(browserTabs)
' set up browsers:
Dim settings As New CefSharp.Settings()
settings.PackLoadingDisabled = True
If CEF.Initialize(settings) Then
web_view1 = New CefSharp.Wpf.WebView()
web_view1.Name = "myTabPage"
web_view1.Address = "http://stackoverflow.com/"
web_view2 = New CefSharp.Wpf.WebView()
web_view2.Name = "browserPage"
web_view2.Address = "https://www.google.com"
web_view2.LifeSpanHandler = Me
web_view2.RequestHandler = Me
AddHandler web_view2.PropertyChanged, AddressOf web2PropChanged
tab1Content.Children.Add(web_view1)
tab2Content.Children.Add(web_view2)
End If
End Sub
End Class
So, in its default state, tab1 is showing at start up -- the browser on tab2 (web_view2) won't initialize until I click its tab or change to its tab via script. Hope this clears it up a bit.
Your code doesn't use Windows Forms' TabPage but perhaps this might still help
As we know, Controls contained in a TabPage are not created until the
tab page is shown, and any data bindings in these controls are not
activated until the tab page is shown. This is by design and you can
call TabPage.Show() method one by one as a workaround.
via MSDN forums
Also, based on the above idea, have you tried the following.
tab2.isEnabled = True
tab1.isEnabled = True
Or:
tab2.Visibility = True
tab1.Visibility = True
Also, the BeginInit Method might help in your situation.
I had the same problem but found it initializes if I use the name of the tabpage and show (tabName1.show()) on the form load code for each page ending with the one I want displayed:
tabPage2.show()
tabPage3.show()
tabPage4.show()
tabPage5.show()
tabPage1.show()
It ripples thru and you never see the pages changing but it works. The other suggestions didn't work for me in Visual Studio 2010.
I have a wpv/mvvm-light/vb.net application with a master/detail view. In this view there is a listbox of clients and a detail view of the client's details where they user can view and edit the customers.
I wanted to add a function where users would be prompted to save changes when a new client is selected in the listbox. If the user chooses yes from the messagebox then save changes and if no then discard changes and return previous selected item back to its original value. I have this all working fine.
My problem is that when the user selects a new client and the messagebox asks them to save changes, the listbox goes out of sync. Meaning that the listbox shows the new client selected but the detail view still shows the previous client. The odd thing is that it works properly on rare occasions.
The following is my view:
<UserControl x:Class="FTC.View.ClientListView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:FTC_Application"
mc:Ignorable="d"
d:DesignHeight="400" d:DesignWidth="900">
<ListBox
Grid.Column="1"
Width="350"
Style="{DynamicResource FTC_ListBox}"
ItemTemplate="{DynamicResource FTC_ClientListTemplate}"
ItemContainerStyle="{DynamicResource FTC_ListItem}"
ItemsSource="{Binding ClientViewSource.View}"
SelectedItem="{Binding Path=Selection, Mode=TwoWay}"
/>
<ContentControl DataContext="{Binding Path=Selection, Mode=TwoWay}" >
<!--all the display stuff goes here for the detail view-->
</ContentControl>
</UserControl>
the following is the property in the viewmodel that the selecteditem of the listbox is bound to. It is also the binding for the content control that displays the details.
Public Property Selection As client
Get
Return Me._Selection
End Get
Set(ByVal value As client)
''capture current value of selection
_PreviousClient = _Selection
''If they are the same,
If value Is _PreviousClient Then
Return
End If
' Note that we actually change the value for now.This is necessary because WPF seems to query the
' value after the change. The list box likes to know that the value did change.
If Me._Selection.HasChanges = True And _Selection.HasErrors = False Then
'If HasChangesPrompt(value) = True Then
' ''user rejects saving changes, exit property
' Return
'End If
If FTCMessageBox.Show("Do you want to save your changes", "Unsaved Changes", MessageBoxButton.YesNo, MessageBoxImage.Warning) = MessageBoxResult.No Then
''SELECTION IS CANCELLED
' change the value back, but do so after the UI has finished it's current context operation.
Application.Current.Dispatcher.BeginInvoke(New Action(Sub()
'' revert the current selected item to its original values and reset its HasCHanges tracking
objHelper.CopyProperties(_OriginalClient, _Selection)
_Selection.HasChanges = False
RaisePropertyChanged(ClientSelectedPropertyName)
''continue with listbox selection changing to the new value for selection
_ClientCollectionViewSource.View.MoveCurrentTo(value)
End Sub), DispatcherPriority.Normal, Nothing)
Return
Else
''save changes to database
SaveExecute()
End If
End If
_Selection = value
_Selection.HasChanges = False
RaisePropertyChanged(ClientSelectedPropertyName)
''clone the unchanged version of the current selected client on na original variable
objHelper.CopyProperties(_Selection, _OriginalClient)
End Set
End Property
SO the idea is that if the user does not want to save changes, an original value of the client is copied (using reflection) over the current value, then the ui is updated and the selection continues on to the new value chosen by the user. However, like I said above, the listbox does not reflect this change even though I tired to hard code it with the following line:
''continue with listbox selection changing to the new value for selection
_ClientCollectionViewSource.View.MoveCurrentTo(value)
I got this solution by working customizing the solution posted HERE
can anyone help me figure out why my listbox goes out of sync when this happens.
Thanks in advance
First:
I can't find the real Problem in your solution, but you have definitly - and I repeat - definitly too much code and logic in your Property Setter. Try move it to other methods and validate your implementation of those many ´if else´ blocks.
Second:
The Setter gets only fired when you select a new Item in your Listbox, but you Raise a Property changes for ´ClientSelectedPropertyName´ and not for ´Selection´ as its supposed to be. Move the property changed alsways to the end of your setter.
Try this. I hope it helps :)
So I have a working example that I think follows the MVVM-Light standard. There is a lot going on so I will try to keep it short and precise.
I ended up using EventToCommand bound to the SelectionChanged event with a ListView(instead of listbox). The EventToCommand required new namespace references as is shown below. I then bound the EventToCommand to a RelayCommand in the view model which in turn calls a private sub that handles the client validation and saves/cancels/ and updates the listview seleceditem as required.
For further information, I have a navigation service that is used to navigat between views in my wpf application. I used the MVVM-Light messanger to send a navigationstarting message that is "recieved" by this view model. Then the same client validation functions are performed and naviagtion is canceled/allowed based on user response to the dialog message thrown. I will no include all of hte navigation code unless requested. The following is the code needed to solve my original question.
<UserControl x:Class="FTC.View.ClientListView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:FTC_Application"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:cmd="http://www.galasoft.ch/mvvmlight"
mc:Ignorable="d"
d:DesignHeight="400" d:DesignWidth="900">
<ListView
Grid.Column="1"
Width="350"
Style="{DynamicResource FTC_ListView}"
ItemTemplate="{DynamicResource FTC_ClientListTemplate}"
ItemContainerStyle="{DynamicResource FTC_ListViewItem}"
ItemsSource="{Binding ClientViewSource.View}"
SelectedItem="{Binding Path=Selection, Mode=TwoWay}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<cmd:EventToCommand Command="{Binding SelectedItemChangedCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ListView>
<ContentControl DataContext="{Binding Path=Selection, Mode=TwoWay}" >
<!-- Display stuff and bound controls go here -->
</ContentControl>
</Grid>
</UserControl>
Then the following is the relevant code (I removed as much code as possible to keep it clear) in my view model:
Imports System.Data
Imports System.ComponentModel
Imports System.Collections.ObjectModel
Imports System.Windows.Threading
Imports GalaSoft.MvvmLight
Imports GalaSoft.MvvmLight.Command
Imports GalaSoft.MvvmLight.Messaging
Imports FTCModel
Imports FTC_Application.FTC.Model
Imports FTC_Application.FTC.View
Imports FTC_Application.FTC.ViewModel
Imports FTC_Application.FTC.MessageBox
Imports FTC_Application.FTC.Helpers
Imports FTC_Application.FTC.MessengerHelper
Namespace FTC.ViewModel
Public Class ClientListViewModel
Inherits ViewModelBase
Implements IDataErrorInfo
#Region "DECLARATIONS"
Public Const ClientCollectionPropertyName As String = "ClientCollection"
Public Const ClientSelectedPropertyName As String = "Selection"
Public Const ClientDetailCollectionPropertyName As String = "ClientDetailCollection"
Public Const ClientPropertyName As String = "Client"
''gets the data from LINQ to ENT Model
Private _Clients As New ObservableCollection(Of client)
''creats holder for the selected item two way binding
Private _Selection As New client
''the following is used to track changes for unding and canceling selection changed
Private _PreviousClient As New client
Private _PreviousOriginalClient As New client
Private _OriginalClient As New client
''Recieves observable collection and provicdes sorting and filtering function
Private _ClientCollectionViewSource As New CollectionViewSource
''RELAY COMMANDS declarations
Private _SaveCommand As RelayCommand
Private _SelectedItemChangedCommand As RelayCommand
''gets the VML for getting the data service
Private vml As ViewModelLocator = TryCast(Application.Current.Resources("Locator"), ViewModelLocator)
''this is a holder for the client data service
Private _ClientAccess As IClientDataService = vml.Client_Service
'' has functions using reflection for copying objects
Dim objHelper As New ObjectHelper
''tracks if client validation is coming from navigation or listview selecteditemchanged
Private bNavigatingFlag As Boolean = False
#End Region
#Region "PROPERTIES"
Public ReadOnly Property ClientViewSource As CollectionViewSource
Get
Return Me._ClientCollectionViewSource
End Get
End Property
Private Property Clients As ObservableCollection(Of client)
Get
Return Me._Clients
End Get
Set(ByVal value As ObservableCollection(Of client))
Me._Clients = value
_Clients = value
RaisePropertyChanged(ClientCollectionPropertyName)
End Set
End Property
Public Property Selection As client
Get
Return Me._Selection
End Get
Set(ByVal value As client)
''capture current value of selection
_PreviousClient = _Selection
objHelper.CopyProperties(_OriginalClient, _PreviousOriginalClient)
''If they are the same,
If value Is _PreviousClient Then
Return
End If
_Selection = value
_Selection.HasChanges = False
RaisePropertyChanged(ClientSelectedPropertyName)
''clone the unchanged version of the current selected client on na original variable
objHelper.CopyProperties(_Selection, _OriginalClient)
End Set
End Property
#End Region
#Region "COMMANDS"
Public ReadOnly Property SelectedItemChangedCommand() As RelayCommand
Get
If _SelectedItemChangedCommand Is Nothing Then
_SelectedItemChangedCommand = New RelayCommand(AddressOf SelectionChangedValidate)
End If
Return _SelectedItemChangedCommand
End Get
End Property
#End Region
#Region "METHODS"
Private Sub SelectionChangedValidate()
''Uses falg to tell if validation request triggered by navigation event or listview selecteditemchanged event
''use previous client for listview event and current client for navigating event
Dim _ClientToValidate As client
If bNavigatingFlag = True Then
_ClientToValidate = _Selection
Else
_ClientToValidate = _PreviousClient
End If
If _ClientToValidate.HasChanges = True And _ClientToValidate.HasErrors = False Then
Dim message = New DialogMessage(_ClientToValidate.chrCompany.ToString + " has been changed." + vbCrLf + "Do you want to save your changes?", AddressOf SavePreviousResponse) With { _
.Button = MessageBoxButton.YesNo, _
.Caption = "Unsaved Changes" _
}
Messenger.[Default].Send(message)
Exit Sub
End If
If _ClientToValidate.HasErrors = True Then
Dim message = New DialogMessage(_ClientToValidate.chrCompany.ToString + " has errors." + vbCrLf + "You must correct these errors before you can continue.", AddressOf HasErrorsResponse) With { _
.Button = MessageBoxButton.OK, _
.Caption = "Validation Error" _
}
Messenger.[Default].Send(message)
Exit Sub
End If
''reset the navigation flag
bNavigatingFlag = False
End Sub
Private Sub SavePreviousResponse(result As MessageBoxResult)
If result = MessageBoxResult.No Then
objHelper.CopyProperties(_PreviousOriginalClient, _PreviousClient)
_PreviousClient.HasChanges = False
Else
''user wants to save changes, save changes to database
SaveExecute()
End If
End Sub
Private Sub HasErrorsResponse(result As MessageBoxResult)
Selection = _PreviousClient
''_ClientCollectionViewSource.View.MoveCurrentTo(_PreviousClient)
End Sub
Private Function HasChangesPrompt(value As client) As Boolean
If FTCMessageBox.Show("Do you want to save your changes", "Unsaved Changes", MessageBoxButton.YesNo, MessageBoxImage.Warning) = MessageBoxResult.No Then
'' change the selected client back to its original value, but do so after the UI has finished its current context operation.
Application.Current.Dispatcher.BeginInvoke(New Action(Sub()
'' revert the current selected item to its original values and reset its HasCHanges tracking
objHelper.CopyProperties(_OriginalClient, _Selection)
_Selection.HasChanges = False
RaisePropertyChanged(ClientSelectedPropertyName)
''continue with listbox selection changing to the new value for selection
_ClientCollectionViewSource.View.MoveCurrentTo(value)
End Sub), DispatcherPriority.Normal, Nothing)
Return True
Else
''user wants to save changes, save changes to database
Return False
SaveExecute()
End If
End Function
#End Region
Public Sub New()
Clients = _ClientAccess.GetClient_All
''Sets the observable collection as the source of the CollectionViewSource
_ClientCollectionViewSource.Source = Clients
If Selection.idClient = 0 Then
Selection = Clients.Item(0)
End If
''register for messages
Messenger.[Default].Register(Of String)(Me, AddressOf HandleMessage)
End Sub
End Class
End Namespace
INXS, you will notice that the selection property setter has way less code/logic. Also, I think each part of the view model is testable and there is no direct coupling between my view and viewmodel. But this is my first WPF/MVVM application so I sitll don't fully grasp all the concepts.
I hope that this can help someone as it took my quite a while to figure it out.
This should be pretty easy, but it throws VS2008 for a serious loop.
I'm trying out WPF with MVVM, and am a total newbie at it although I've been developing for about 15 years, and have a comp. sci. degree. At the current client, I am required to use VB.Net.
I have renamed my own variables and removed some distractions in the code below, so please forgive me if it's not 100% syntactically perfect! You probably don't really need the code to understand the question, but I'm including it in case it helps.
I have a very simple MainView.xaml file:
<Window x:Class="MyApp.Views.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Main Window" Height="400" Width="800" Name="MainWindow">
<Button Name="Button1">Show Grid</Button>
<StackPanel Name="teststack" Visibility="Hidden"/>
</Window>
I also have a UserControl called DataView that consists of a DataGrid:
<UserControl x:Class="MyApp.Views.DataView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:WpfToolkit="http://schemas.microsoft.com/wpf/2008/toolkit" >
<Grid>
<WpfToolkit:DataGrid
ItemsSource="{Binding Path=Entries}" SelectionMode="Extended">
</WpfToolkit:DataGrid>
</Grid>
</UserControl>
The constructor for the DataView usercontrol sets up the DataContext by binding it to a view model, as shown here:
Partial Public Class DataView
Dim dataViewModel As ViewModels.DataViewModel
Public Sub New()
InitializeComponent()
dataViewModel = New ViewModels.DataViewModel
dataViewModel.LoadDataEntries()
DataContext = dataViewModel
End Sub
End Class
The view model for DataView looks like this (there isn't much in ViewModelBase):
Public Class DataViewModel
Inherits ViewModelBase
Public Sub New()
End Sub
Private _entries As ObservableCollection(Of DataEntryViewModel) = New ObservableCollection(Of DataEntryViewModel)
Public ReadOnly Property Entries() As ObservableCollection(Of DataEntryViewModel)
Get
Return _entries
End Get
End Property
Public Sub LoadDataEntries()
Dim dataEntryList As List(Of DataEntry) = DataEntry.LoadDataEntries()
For Each dataentry As Models.DataEntry In dataEntryList
_entries.Add(New DataEntryViewModel(dataentry))
Next
End Sub
End Class
Now, this UserControl works just fine if I instantiate it in XAML. When I run the code, the grid shows up and populates it just fine.
However, the grid takes a long time to load its data, and I want to create this user control programmatically after the button click rather than declaratively instantiating the grid in XAML. I want to instantiate the user control, and insert it as a child of the StackPanel control:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
Dim dataView As New DataView
teststack.Children.Add(dataView)
End Sub
When I do this, as soon as the Button1_Click finishes, my application locks up, starts eating RAM, and hits the CPU about 50%.
Am I not instantiating my UserControl properly? It all seems to come down to the DataContext assignment in DataEntry's constructor. If I comment that out, the app works as expected (without anything in the grid, of course).
If I move this code block into Button1_Click (basically moving DataEntry's constructor code up a level), the app still fails:
dataViewModel = New ViewModels.DataViewModel
dataViewModel.LoadDataEntries()
dataView.DataContext = dataViewModel
I'm stumped. Can anybody give me some tips on what I could be doing wrong, or even how to debug what infinite loop my app is getting itself into?
Many thanks.
The root cause of your issue appears to be either the raw amount of data you're loading or some inefficiency in how you load that data. Having said that, the reason you're seeing the application lock up is that you're locking the UI thread when loading the data.
I believe that in your first case the data loading has been off loaded onto another thread to load the data. In you second example you're instantiating the control on the UI thread and as a result all the constructor and loading logic is performed on the current thread (the UI thread). If you offload this work onto another thread then you should see similar results to the first example.
I eventually gave up on trying to get the DataContext on the UserControl set during instantiation of the UserControl (either in XAML or code). Now I load up the data and set the DataContext of the UserControl in an event in the UserControl (IsVisibleChanged, I believe). When I instantiate the UserControl in XAML, I have it's Visibility set to Hidden. When Button1 is clicked, I set the UserControl's Visibility to Visible. So the UserControl pops into view, and it loads up its data and DataContext is set. Seems to work, but also seems very kludgey. :-( Thanks for the help, folks!
If it's only a matter of your control taking a long time to populate data, you should populate the control on another thread then add it through a delegate:
Since I'm not too good at writing VB.NET, but here's the C# equivalent:
private void Button1_Click(Object sender, RoutedEventArgs e)
{
Thread thr = new Thread(delegate()
{
DataView dataView = new DataView();
this.Dispatcher.BeginInvoke((Action) delegate()
{
teststack.Children.Add(dataView);
});
});
thr.Start();
}