gpt4 book ai didi

WPF区分编码-SelectionChanged和鼠标-SelectionChanged

转载 作者:行者123 更新时间:2023-12-04 21:51:03 25 4
gpt4 key购买 nike

我有一个权谋的问题(对我来说)。在我的 WPF 应用程序中,我有一个 ListBox,它在 ItemTemplate 中有一个 Combobox。当用户选择 ComboBoxItem 时,我必须对作为 ListBox 的 ItemsSource 的 ObservableCollection 执行一些复杂的操作,然后我必须显示带有更改数据的 ListBox。问题是,如果我处理 ComboBox 控件的事件“SelectionChanged”,每次我修改 comboboxItems 的源类时,我都会在处理该事件的方法中输入,这会产生错误的结果。简而言之,我必须以某种方式区分代码生成的 SelectionChanged 和用户使用鼠标手动生成的 SelectionChanged。我尝试了很多方法,但没有任何效果:-(

我认为最好的解决方案是处理 Combo 的 ItemContainerStyle 的 ContentPresenter 的事件“GotFocus”或“MouseUp”,或者处理相同的事件(“GotFocus”和“MouseUp”) Combo 的 ItemsPanel,但我处理的方法没有捕获事件(在调试中,光标根本没有进入方法)。

在“第一轮”完成之前,我不能使用 bool 值来停止方法“SelectionChanged”,因为 ComboBoxItems 的源类的更改发生在该方法全部执行之后。

Combos 的默认值并不总是第一个(这太容易了:-)),也不总是相同的。每次用户选择其中一个 Combo 的项目时,其他 Combo 的默认值必须更改。

你能帮帮我吗?皮莱吉

' 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

最佳答案

首先,我认为最好在项目容器本身上处理事件,而不是在项目内的内容呈现器上处理事件。现在我想起来了,这可能就是您看不到事件的原因。容器可能正在吃事件以供选择。

但是如果您无法捕获 MouseDown/GotFocus 事件,则可以使用 PreviewMouseDown/PreviewGotFocus 事件。以防万一您不确定这些意味着什么,您应该阅读 wpf 事件路由体系结构以及冒泡和隧道事件。

关于WPF区分编码-SelectionChanged和鼠标-SelectionChanged,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3693168/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com