I am trying to use MVVM in Silverlight, but I am quite new to it so I am not quite sure on some things. I have a silverlight page which displays the progress of some server side operation. The current progress comes from a web service and should be refreshed every few seconds (lets say 10 seconds for the sake of the argument).
What is the best way to implement this? The options I could think of was:
Initalize a DispatcherTimer in the Initalize method of my ViewModel and refresh the view from the DispatcherTimer event (putting the timer details in the ViewModel)
Create a wrapper arround DispatcherTimer (e.g. PeriodicCommandExecutor) which would be a Control or resource similar to the Timer control in WindowsForms with a command property that I bind to a Refresh command in the ViewModel (putting the timer details in the View)
I think the second option is preferred, because it makes the ViewModel easier to test and DispatcherTimer is an UI implementation detail which I don't want in my ViewModel propably. Do you agree?
If yes, how would you create such a wrapper. I started doing an DependencyObject with attached properties, but I am not sure how to forward the property values like Interval to the internal DispatcherTimer. Silverlight doesn't seem to provide any events when the dependency properties change and DispatcherTimer is not a DependencyObject so I can't databind directly to its properties.
Thanks!
Why use a DispatcherTimer? Why not use an ordinary System.Threading.Timer, which will fire its callback on a background thread?
If you put your UI progress update somewhere inconspicious (i.e. not in the centre of the UI, maybe in a bottom corner or status bar), then have the background timer chugging away while the user carries on with what they were doing. The progress value can be populated into the viewmodel, and shown on the UI using binding. This way you don't have to tie up the UI thread making web service calls.
At the end I solved my dillema creating a behavior which periodically executes a refresh command on the ViewModel which you can specify.
The code for the behavior is like this
(sorry for VB code):
Option Strict On
Imports System.Windows.Threading
Imports System.Windows.Interactivity
Namespace View.Behaviors
Public Class RefreshBehavior
Inherits Behavior(Of FrameworkElement)
Public Property Command As ICommand
Get
Return DirectCast(GetValue(CommandProperty), ICommand)
End Get
Set(ByVal value As ICommand)
SetValue(CommandProperty, value)
End Set
End Property
Public Shared ReadOnly CommandProperty As DependencyProperty = _
DependencyProperty.Register("Command", _
GetType(ICommand), GetType(RefreshBehavior), _
New PropertyMetadata(Nothing))
Public Property CommandParameter As Object
Get
Return GetValue(CommandParameterProperty)
End Get
Set(ByVal value As Object)
SetValue(CommandParameterProperty, value)
End Set
End Property
Public Shared ReadOnly CommandParameterProperty As DependencyProperty = _
DependencyProperty.Register("CommandParameter", _
GetType(Object), GetType(RefreshBehavior), _
New PropertyMetadata(Nothing))
Public Property Interval As TimeSpan
Get
Return DirectCast(GetValue(IntervalProperty), TimeSpan)
End Get
Set(ByVal value As TimeSpan)
SetValue(IntervalProperty, value)
End Set
End Property
Public Shared ReadOnly IntervalProperty As DependencyProperty = _
DependencyProperty.Register("Interval", _
GetType(TimeSpan), GetType(RefreshBehavior), _
New PropertyMetadata(TimeSpan.Zero, AddressOf OnIntervalUpdate))
Public Property Enabled As Boolean
Get
Return DirectCast(GetValue(EnabledProperty), Boolean)
End Get
Set(ByVal value As Boolean)
SetValue(EnabledProperty, value)
End Set
End Property
Public Shared ReadOnly EnabledProperty As DependencyProperty = _
DependencyProperty.Register("Enabled", _
GetType(Boolean), GetType(RefreshBehavior), _
New PropertyMetadata(False, AddressOf OnEnabledUpdate))
Dim WithEvents timer As New DispatcherTimer()
Private Shared Sub OnEnabledUpdate(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
Dim enable As Boolean = CType(e.NewValue, Boolean)
Dim executor As RefreshBehavior = CType(d, RefreshBehavior)
If Not executor.attached Then Return
Dim timer As DispatcherTimer = executor.timer
If enable AndAlso Not timer.IsEnabled Then
timer.Start()
ElseIf Not enable AndAlso Not timer.IsEnabled Then
timer.Stop()
End If
End Sub
Private Shared Sub OnIntervalUpdate(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
Dim executor As RefreshBehavior = CType(d, RefreshBehavior)
Dim timer As DispatcherTimer = executor.timer
timer.Interval = CType(e.NewValue, TimeSpan)
End Sub
Private WithEvents attachedObject As FrameworkElement
Private Sub OnUnload(ByVal sender As Object, ByVal e As EventArgs) Handles attachedObject.Unloaded
timer.Stop()
End Sub
Private attached As Boolean = False
Protected Overrides Sub OnAttached()
attached = True
attachedObject = AssociatedObject
If Enabled Then timer.Start()
MyBase.OnAttached()
End Sub
Protected Overrides Sub OnDetaching()
timer.Stop()
attached = False
attachedObject = Nothing
MyBase.OnDetaching()
End Sub
Private Sub OnTick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer.Tick
Dim cmd = Command
Dim parameter = CommandParameter
If Interval < TimeSpan.MaxValue AndAlso cmd IsNot Nothing AndAlso cmd.CanExecute(parameter) Then
cmd.Execute(parameter)
End If
End Sub
End Class
End Namespace
You can use it like this:
<i:Interaction.Behaviors>
<Behaviors:RefreshBehavior Enabled="True" Interval="0:0:10" Command="{Binding RefreshPageCommand}" />
</i:Interaction.Behaviors>
I hope it helps someone with a similar problem.
Related
The (deprecated) Microsoft Forms 2.0 controls include a combobox which provides an invaluable property: .TopIndex (documentation here).
It seems that this property is not available with standard comboboxes in forms in Microsoft Access 2019 (according to the documentation), and is not available with standard comboboxes in Windows Forms (.NET) (here, ComboBox inherits from ListControl and does not provide this property, while ListBox also inherits from ListControl, but provides it).
I have a lot of old code which heavily relies on the .TopIndex property, and it's time to move that code to other technologies.
So I'd like to know if I have missed something in the documentation and if there is an equivalent property with another name which I could use to determine which items are visible in the list part of a combobox. I'd like to know this for comboboxes in Access 2019 (I am not that hostile towards this application as many others here are) as well as for comboboxes in Windows Forms.
I am aware that there are a lot of free and commercial controls (including comboboxes) with enhanced functionality for Windows Forms. I will definitely go that way (or write my own) unless I have missed something in the documentation.
However, the situation is completely different when it comes to Access 2019 forms. I could not find a single free third-party ActiveX / COM combobox which I could use on Access forms and which provides this functionality. Theoretically, I probably could write an ActiveX / COM control using .NET and then use it on Access 2019 forms, but this seems quite painful.
As far as .Net WinForm ComboBox goes, you have not missed anything as the functionality of the TopIndex property is not implemented. That said, it is pretty straight forward to extend the base ComboBox control to add this property. The following example control should get you started.
This control attaches a listener to the native ListBox dropdown and updates the TopIndex property on WM_VSCROLL and the LB_SETCARETINDEX (this captures the initial position on opening) messages. Additionally, the base SelectedIndexChange event is used to capture changes due to keyboard actions (pgUp/pgDn, Arrow up/down). The TopIndex property is retained after the dropdown closes and is reset on opening the dropdown. The control also exposes a TopIndexChanged event.
Imports System.Runtime.InteropServices
Public Class ComboBoxEx : Inherits ComboBox
Private listBoxListener As ListBoxNativeWindow
Public Event TopIndexChanged As EventHandler(Of ComboBoxTopIndexArg)
Private _TopIndex As Int32 = -1
Public Sub New()
MyBase.New
listBoxListener = New ListBoxNativeWindow(Me)
End Sub
Public Property TopIndex As Int32
Get
Return _TopIndex
End Get
Private Set(value As Int32)
If value <> _TopIndex Then
_TopIndex = value
RaiseEvent TopIndexChanged(Me, New ComboBoxTopIndexArg(value))
End If
End Set
End Property
Protected Overrides Sub OnDropDown(e As EventArgs)
_TopIndex = -1 ' reset on opening the listbox
MyBase.OnDropDown(e)
End Sub
Private Class ListBoxNativeWindow : Inherits NativeWindow
Private listBoxHandle As IntPtr
Private TopIndex As Int32
Private parent As ComboBoxEx
Public Sub New(ByVal parent As ComboBoxEx)
Me.parent = parent
WireParent()
If parent.IsHandleCreated Then
GetListBoxHandle()
AssignHandle(listBoxHandle)
End If
End Sub
Private Sub WireParent()
AddHandler parent.HandleCreated, AddressOf Me.OnHandleCreated
AddHandler parent.HandleDestroyed, AddressOf Me.OnHandleDestroyed
AddHandler parent.SelectedIndexChanged, AddressOf UpdateTopIndexOnIndexChanged
End Sub
Private Sub OnHandleCreated(ByVal sender As Object, ByVal e As EventArgs)
GetListBoxHandle()
AssignHandle(listBoxHandle)
End Sub
Private Sub OnHandleDestroyed(ByVal sender As Object, ByVal e As EventArgs)
ReleaseHandle()
End Sub
Private Sub UpdateTopIndexOnIndexChanged(sender As Object, e As EventArgs)
SetParentTopIndex()
End Sub
Private Sub GetListBoxHandle()
Const CB_GETCOMBOBOXINFO As Int32 = &H164
Dim info As New ComboBoxInfo
info.cbSize = Marshal.SizeOf(info)
Dim res As Boolean = SendMessage(Me.parent.Handle, CB_GETCOMBOBOXINFO, Nothing, info)
listBoxHandle = info.hwndList
End Sub
Protected Overrides Sub WndProc(ByRef m As Message)
Const WM_VSCROLL As Int32 = &H115
Const LB_SETCARETINDEX As Int32 = &H19E
MyBase.WndProc(m)
If m.Msg = WM_VSCROLL OrElse m.Msg = LB_SETCARETINDEX Then
SetParentTopIndex()
End If
End Sub
Private Sub SetParentTopIndex()
Const LB_GETTOPINDEX As Int32 = &H18E
parent.TopIndex = SendMessage(listBoxHandle, LB_GETTOPINDEX, IntPtr.Zero, IntPtr.Zero)
End Sub
End Class
Public Class ComboBoxTopIndexArg : Inherits EventArgs
Public Sub New(topIndex As Int32)
Me.TopIndex = topIndex
End Sub
Public ReadOnly Property TopIndex As Int32
End Class
#Region "NativeMethods"
<StructLayout(LayoutKind.Sequential)>
Private Structure ComboBoxInfo
Public cbSize As Int32
Public rcItem As RECT
Public rcButton As RECT
Public stateButton As IntPtr
Public hwndCombo As IntPtr
Public hwndEdit As IntPtr
Public hwndList As IntPtr
End Structure
<StructLayout(LayoutKind.Sequential)>
Private Structure RECT
Public Left, Top, Right, Bottom As Int32
End Structure
<DllImport("user32.dll")>
Private Shared Function SendMessage(hWnd As IntPtr, Msg As Int32, wParam As IntPtr, <Out()> ByRef lParam As ComboBoxInfo) As Boolean
End Function
<DllImport("user32.dll")>
Private Shared Function SendMessage(hWnd As IntPtr, Msg As Int32, wParam As IntPtr, lParam As IntPtr) As Int32
End Function
#End Region
End Class
I leave it to you to wrap this in an ActiveX exposed wrapper for use in Access. Doing so is fairly easy using the templates in the Microsoft InteropForms Toolkit 2.1. Just note that those templates are setup using the "Any CPU" platform and you will need to change that to "x86".
I have an ICollectionView declared like so;
Public Shared ReadOnly DataIcvProperty As DependencyProperty = DependencyProperty.Register("DataIcv", GetType(ICollectionView), GetType(DataNavigator), New PropertyMetadata(Nothing))
<Description("The CollectionView (as an ICollectionView) to be passed to the DataNavigator control"), Category("Navigation Data Source")>
Public Property DataIcv As ICollectionView
Get
Return GetValue(DataIcvProperty)
End Get
Set(ByVal Value As ICollectionView)
SetValue(DataIcvProperty, Value)
End Set
End Property
On which I would like to keep track of the CurrentChanged event. So in the constructor of a wpf userControl (in which this property is declared I have added the following:
AddHandler DataIcv.CurrentChanged, AddressOf OnDataICVCurrentChanged
and then added the following
Public Sub OnDataICVCurrentChanged(ByVal sender As Object, e As EventArgs) Handles internalIcv.CurrentChanged
'Do whatever needs doing when the record in the ICollectionView changes
End Sub
When I then compile the usercontrol and use it in a separate project I get a NullReferenceException on the AddHandler line in the Constructor of the control (without any inner exception details).
What do I need to do to keep track of the current Changed event of the DataIcv so that I can have elements of my user control react properly to those changes?
Thanks
EDIT
This relates to the issue I described here. Essentially I want the navigator control to reflect the fact that the end user might select rows on the grid independently of the navigator.
The problem is, when you try to subscribe to the CurrentChanged event in your constructor, the DependencyProperty DataIcvProperty has a default value Nothing (as you specified in your property registration). So the NullReferenceException.
You could solve this with a PropertyChangeCallback (see documentation).
I come from the C# world and cannot guarantee the right syntax for VB.NET, but this approach surely will work:
Public Shared ReadOnly DataIcvProperty As DependencyProperty =
DependencyProperty.Register("DataIcv",
GetType(ICollectionView), GetType(DataNavigator),
New FrameworkPropertyMetadata(
Nothing,
New PropertyChangedCallback(AddressOf OnDataIcvChanged)))
Then you need to implement the OnDataIcvChanged static method, which will be called every time the property value changes.
Private Shared Sub OnDataIcvChanged(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
If e.OldValue IsNot Nothing
RemoveHandler (e.OldValue As ICollectionView).CurrentChanged, AddressOf OnDataICVCurrentChanged
EndIf
If e.NewValue IsNot Nothing
AddHandler (e.NewValue As ICollectionView).CurrentChanged, AddressOf OnDataICVCurrentChanged
EndIf
End Sub
Update:
If your event handler method is not static, then you should access it via the object instance:
Private Shared Sub OnDataIcvChanged(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
Dim DataNavigator instance = d As DataNavigator
If e.OldValue IsNot Nothing
RemoveHandler (e.OldValue As ICollectionView).CurrentChanged, AddressOf instance.OnDataICVCurrentChanged
EndIf
If e.NewValue IsNot Nothing
AddHandler (e.NewValue As ICollectionView).CurrentChanged, AddressOf instance.OnDataICVCurrentChanged
EndIf
End Sub
Edit: Code That eventually worked:
Public Shared ReadOnly DataIcvProperty As DependencyProperty = DependencyProperty.Register("DataIcv", GetType(ICollectionView), GetType(DataNavigator), New FrameworkPropertyMetadata(Nothing, New PropertyChangedCallback(AddressOf OnDataIcvChanged)))
<Description("The CollectionView (as an ICollectionView) to be passed to the DataNavigator control"), Category("Navigation Data Source")>
Public Property DataIcv As ICollectionView
Get
Return CType(GetValue(DataIcvProperty), ICollectionView)
End Get
Set(ByVal Value As ICollectionView)
SetValue(DataIcvProperty, Value)
End Set
End Property
Private Shared Sub OnDataIcvChanged(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
Dim dn As DataNavigator = CType(d, DataNavigator)
If e.OldValue IsNot Nothing Then
RemoveHandler dn.DataIcv.CurrentChanged, AddressOf dn.OnDataICVCurrentChanged
End If
If e.NewValue IsNot Nothing Then
AddHandler dn.DataIcv.CurrentChanged, AddressOf dn.OnDataICVCurrentChanged
End If
End Sub
Private Sub OnDataICVCurrentChanged(ByVal sender As Object, ByVal e As EventArgs)
Record.Text = (DataIcv.CurrentPosition + 1).ToString
End Sub
I've searched as best as I can, and I can't find an answer to this specific problem that I have... WPF binding seems to be great and all, but I end up banging my head against the wall more often than not.
Okay, I have a singleton class which is ultimately the one that I'm binding to:
Imports System.ComponentModel
Imports System.Collections.ObjectModel
Public Class AmandaSeyfried
Implements INotifyPropertyChanged
Shared _config As New config
Public Event PropertyChanged(sender As Object, E As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged
Protected Overridable Sub OnPropertyChanged(propertyName As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
Private Shared _thisInstance As AmandaSeyfried
Protected Sub New()
' initialization goes here
End Sub
Public Shared Function GetSingleton() As AmandaSeyfried
' initialize object if it hasn't already been done
If _thisInstance Is Nothing Then
_thisInstance = New AmandaSeyfried
End If
' return the initialized instance
Return _thisInstance
End Function
Public Class CountryTranslation
Implements INotifyPropertyChanged
Private Property _englishCountryName As String = ""
Public Property EnglishCountryName As String
Get
Return _EnglishCountryName
End Get
Set(value As String)
If _englishCountryName <> value Then
_englishCountryName = value
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("EnglishCountryName"))
End If
End Set
End Property
Private Property _foreignCountryName As String = ""
Public Property ForeignCountryName As String
Get
Return _foreignCountryName
End Get
Set(value As String)
If _foreignCountryName <> value Then
_foreignCountryName = value
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("ForeignCountryName"))
End If
End Set
End Property
Public Event PropertyChanged(sender As Object, e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
End Class
Private WithEvents _countryTranslations As New ObservableCollection(Of CountryTranslation)
Public Property CountryTranslations As ObservableCollection(Of CountryTranslation)
Get
If _config.GetKeyTextValue("countryTranslations") <> "" Then
Dim reader As New IO.StringReader(_config.GetKeyTextValue("countryTranslations"))
Dim Serializer As New Xml.Serialization.XmlSerializer(_countryTranslations.GetType)
_countryTranslations = Serializer.Deserialize(reader)
End If
Return _countryTranslations
End Get
Set(value As ObservableCollection(Of CountryTranslation))
_countryTranslations = value
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("CountryTranslations"))
End Set
End Property
Private Sub CountryTranslationCollectionChanged(sender As Object, e As Specialized.NotifyCollectionChangedEventArgs) Handles _countryTranslations.CollectionChanged
Dim newStringWriter As New IO.StringWriter
Dim NewSerializer As New Xml.Serialization.XmlSerializer(_countryTranslations.GetType)
NewSerializer.Serialize(newStringWriter, _countryTranslations)
_config.SaveKeyTextValue("countryTranslations", newStringWriter.ToString)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("CountryTranslations"))
End Sub
End Class
_config is a misnamed helper class that stores and retrieves data from a local SqlCe instance. Essentially the object is serialized, stored in the DB, and then pulled out of the DB any times it's needed and deserialized back into an object. All in all, it seems to be working fairly well.
My problem is that although I can bind to the object, and I can monitor when a row is added in a WPF DataGrid via the CollectionChangedMethod handler, I don't get any notification when either of the two properties of CountryTranslation are changed.
The rest of my related code is... XAML... there's obviously more, but I don't believe the XAML portion of the binding is to blame, so I'll trim it to the relevant:
<toolkit:DataGrid Margin="12,12,12,12" ItemsSource="{Binding Path=KarenSmith.CountryTranslations, Mode=TwoWay}" AutoGenerateColumns="False" HorizontalAlignment="Stretch" Width="Auto" SelectionMode="Single">
<toolkit:DataGrid.Columns>
<toolkit:DataGridTextColumn Width="283" Binding="{Binding EnglishCountryName,Mode=TwoWay}" />
<toolkit:DataGridTextColumn Width="283" Binding="{Binding ForeignCountryName,Mode=TwoWay}" />
</toolkit:DataGrid.Columns>
</toolkit:DataGrid>
And the nice and simple code-behind:
Public Class Preferences
Public Property KarenSmith As AmandaSeyfried = AmandaSeyfried.GetSingleton
Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
DataContext = Me
End Sub
Private Sub Close_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
Me.Close()
End Sub
End Class
If I throw some break points on the Getter and Setters of the CountryTranslation class, I can monitor when they're being changed (via the datagrid, so binding is working), but try as I might I can't figure out how to raise an event based upon that back in the main class to subsequently update the datastore to show the changes.
Usually I add a CollectionChanged event to the ObservableCollection, which attaches a PropertyChanged event to it's items when they get added, and that event listener listens for changes and would handle them as needed.
Here's an example: (hope the syntax is correct since I just ran it through a C# to VB.Net converter)
Public Sub New()
AddHandler MyCollection.CollectionChanged, AddressOf MyCollection_CollectionChanged
End Sub
Private Sub MyCollection_CollectionChanged(sender As Object, e As CollectionChangedEventArgs)
If e.NewItems IsNot Nothing Then
For Each item As MyItem In e.NewItems
AddHandler item.PropertyChanged, AddressOf MyItem_PropertyChanged
Next
End If
If e.OldItems IsNot Nothing Then
For Each item As MyItem In e.OldItems
RemoveHandler item.PropertyChanged, AddressOf MyItem_PropertyChanged
Next
End If
End Sub
Private Sub MyItem_PropertyChanged(sender As Object, e As PropertyChangedEventArgs)
If e.PropertyName = "Some Property" Then
DoWork()
End If
End Sub
The C# version looks like this:
public MyViewModel()
{
MyCollection.CollectionChanged += MyCollection_CollectionChanged;
}
void MyCollection_CollectionChanged(object sender, CollectionChangedEventArgs e)
{
if (e.NewItems != null)
foreach(MyItem item in e.NewItems)
item.PropertyChanged += MyItem_PropertyChanged;
if (e.OldItems != null)
foreach(MyItem item in e.OldItems)
item.PropertyChanged -= MyItem_PropertyChanged;
}
void MyItem_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "Some Property")
DoWork();
}
I'm trying to get the WebBrowser control to display HTML loaded from a variable. I found this solution Displaying html from string in WPF WebBrowser control so am trying to implement my first DependencyProperty.
Here's my attempt at the implementation:
Public Shared ReadOnly HtmlProperty As DependencyProperty = DependencyProperty.RegisterAttached("Html", GetType(String), GetType(PortraitSingle), New FrameworkPropertyMetadata(OnHtmlChanged))
<AttachedPropertyBrowsableForType(GetType(WebBrowser))> _
Public Shared Function GetHtml(ByVal d As WebBrowser) As String
Return DirectCast(d.GetValue(HtmlProperty), String)
End Function
Public Shared Sub SetHtml(ByVal d As WebBrowser, ByVal value As String)
d.SetValue(HtmlProperty, value)
End Sub
Private Shared Sub OnHtmlChanged(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
Dim wb As WebBrowser = TryCast(d, WebBrowser)
If wb IsNot Nothing Then
wb.NavigateToString(TryCast(e.NewValue, String))
End If
End Sub
and the XAML
<WebBrowser x:Name="HTMLDescription" Grid.RowSpan="2" Margin="10" local:PortraitSingle.Html="{Binding HtmlToDisplay}"></WebBrowser>
First off, the problem I'm having is I'm getting the following errors:
Argument not specified for parameter 'e' of 'Private Shared Sub OnHtmlChanged(d As System.Windows.DependencyObject, e As System.Windows.DependencyPropertyChangedEventArgs)
and
The attachable property 'Html' was not found in type 'PortraitSingle'. (PortraitSingle is the name of my Window).
I can't work out how to get around them.
Also, I can't understand how I actually load my HTML into the WebBrowser control.
Ben
Found my problem.
Needed to add the 'AddressOf' to the DependencyProperty statement
Public Shared ReadOnly HtmlProperty As DependencyProperty = DependencyProperty.RegisterAttached("Html", GetType(String), GetType(PortraitSingle), New FrameworkPropertyMetadata(AddressOf OnHtmlChanged))
I have a machiavellian question (for me).
In my WPF application I have a ListBox that has in the ItemTemplate a Combobox. When the user select a ComboBoxItem, I have to do some complex operations on the ObservableCollection that is the ItemsSource of the ListBox, then I have to show the ListBox with the changed data. The problem is that if I handle the event "SelectionChanged" of the ComboBox control, every time I modify the source-class of the comboboxItems I enter in the method that handle the event, and this generate wrong results. In short I have to distinguish, in some way, between the SelectionChanged generated by code, and the SelectionChanged generated manually by the user with the mouse.
I have tried many ways, but nothing that works :-(
The soution I thought was the best, is to handle the event "GotFocus" or "MouseUp" of the ContentPresenter of the ItemContainerStyle of the Combo, or else to handle the same events ("GotFocus" and "MouseUp") of the ItemsPanel of the Combo, but the method I handled didn't capture the event (in debug the cursor doesn't enter at all in the method).
I can't use a boolean to stop the method "SelectionChanged" until the "first round" is finished, because the changes of the source-class of the ComboBoxItems occurs after that the method has been all executed.
The default value of the Combos is not always the first (it would be too easy :-)), and not always the same. Everytime the user select an item of one of the Combo, the default value of the other Combos has to change.
Can you help me?
Pileggi
' XAML
<Style x:Key="modComboCriteriEventParts" TargetType="{x:Type ComboBox}">
<EventSetter Event="Selector.SelectionChanged" Handler="cb_SelectionChanged"/>
</Style>
<DataTemplate x:Key="modLBoxCriteriParts">
<ComboBox Style = "{StaticResource modComboCriteriEventParts}"
ItemsSource = "{Binding CriteriItemList}"
ItemContainerStyle = "{DynamicResource modComboContainerParts}"
SelectedIndex = "{Binding valueSelected}" ... />
</DataTemplate>
<ListBox x:Name="lbCriteri" IsSynchronizedWithCurrentItem="True"
ItemsSource = "{Binding CriteriList, Source={StaticResource P_CriteriDataSource}}"
ItemTemplate = "{DynamicResource modLBoxCriteriParts}"
... />
' Code Behind
Private Sub cb_SelectionChanged(ByVal sender As System.Object, ByVal e As SelectionChangedEventArgs)
Dim ri as New RicambiCriteriList() As ObservableCollection(Of P_CriteriItem)
' some complex operations with ri ...
be = BindingOperations.GetBindingExpression(Me.lbCriteri, ListBox.ItemsSourceProperty)
Dim allCriteri As P_Criteri = DirectCast(be.DataItem, P_Criteri)
allCriteri.AddData (ri)
e.Handled = True
End Sub
' Source-Class
Public Class P_Criteri
Private _CriteriList As New ObservableCollection(Of P_CriteriItem)
Public ReadOnly Property CriteriList() As ObservableCollection(Of P_CriteriItem)
Get
CriteriList = _CriteriList
End Get
End Property
Public Sub AddData(ByVal CriteriListPass As ObservableCollection(Of P_CriteriItem))
_CriteriList.Clear()
For Each a As P_CriteriItem In CriteriListPass
_CriteriList.Add(a)
Next
End Sub
End Class
Public Class P_CriteriItem
Implements INotifyPropertyChanged
Public Sub New(ByVal criterioPass As String, ByVal CriteriItemListPass As ObservableCollection(Of P_CriteriItemValore), _
ByVal widthCriteriValuesPass As Double)
Me._criterio = criterioPass
Me._CriteriItemList = CriteriItemListPass
Me._widthCriteriValues = widthCriteriValuesPass
End Sub
Private _criterio As String = ""
Private _CriteriItemList As New ObservableCollection(Of P_CriteriItemValore)
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Public Property criterio() As String
Get
Return Me._criterio
End Get
Set(ByVal value As String)
If Not Object.Equals(Me._criterio, value) Then
Me._criterio = value
Me.OnPropertyChanged ("criterio")
End If
End Set
End Property
Public Property CriteriItemList() As ObservableCollection(Of P_CriteriItemValore)
Get
Return Me._CriteriItemList
End Get
Set(ByVal value As ObservableCollection(Of P_CriteriItemValore))
If Not Object.Equals(Me._CriteriItemList, value) Then
Me._CriteriItemList = value
Me.OnPropertyChanged ("CriteriItemList")
End If
End Set
End Property
Protected Overridable Sub OnPropertyChanged(ByVal propertyName As String)
Dim handler As PropertyChangedEventHandler = Me.PropertyChangedEvent
If handler IsNot Nothing Then
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End If
End Sub
End Class
Public Class P_CriteriItemValore
Implements INotifyPropertyChanged
Public Sub New(ByVal criterioValorePass As String)
Me._criterioValore = criterioValorePass
End Sub
Private _criterioValore As String = Nothing
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Public Property criterioValore() As String
Get
Return Me._criterioValore
End Get
Set(ByVal value As String)
If Not Object.Equals(Me._criterioValore, value) Then
Me._criterioValore = value
Me.OnPropertyChanged ("criterioValore")
End If
End Set
End Property
Protected Overridable Sub OnPropertyChanged(ByVal propertyName As String)
Dim handler As PropertyChangedEventHandler = Me.PropertyChangedEvent
If handler IsNot Nothing Then
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End If
End Sub
End Class
Firstly I think its better to handle events on the item container itself and not on the content presenter within the item. And now that I think of it that's probably why you don't see the events. The container is probably eating the events for selection.
But either way if you can't catch the MouseDown/GotFocus events, you can use the PreviewMouseDown/PreviewGotFocus events. Just in case you are not sure what these mean you should read up on wpf event routing architecture and bubbling and tunneling events.