我试图根据绑定的布尔值在组合框列表中显示图像。单击图像时的布尔值,因此图像应该更改。 这是xaml:
<ComboBox Name="Combo2" Margin="20,79,20,0" ItemsSource="{Binding}" VerticalAlignment="Top" Height="20">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Name="MyBoolImage" Height="12" Width="12" MouseLeftButtonUp="Image_MouseLeftButtonUp"/>
<TextBlock Text="{Binding name}" Margin="5,0,0,0" Width="100" />
</StackPanel>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding LightOn}" Value="False">
<Setter TargetName="MyBoolImage" Property="Source" Value="/Images/Exit.png"/>
</DataTrigger>
<DataTrigger Binding="{Binding LightOn}" Value="True">
<Setter TargetName="MyBoolImage" Property="Source" Value="/Images/Cut.png"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
数据类:
Class ComboData
Private _LightOn As Boolean
Public Property LightOn As Boolean
Get
Return _LightOn
End Get
Set(value As Boolean)
_LightOn = value
End Set
End Property
Private _name As String
Public Property name As String
Get
Return _name
End Get
Set(value As String)
_name = value
End Set
End Property
Sub New(name_ As String, Light_On As Boolean)
_LightOn = Light_On
_name = name_
End Sub
End Class
加载一些测试数据:
Dim x As New List(Of ComboData)
x.Add(New ComboData("test1a", True))
x.Add(New ComboData("test2a", False))
x.Add(New ComboData("test3a", True))
x.Add(New ComboData("test4a", True))
x.Add(New ComboData("test5a", False))
x.Add(New ComboData("test6a", True))
Combo2.ItemsSource = x
最后是魔术未发生的点击事件......
Private Sub Image_MouseLeftButtonUp(sender As Object, e As MouseButtonEventArgs)
Dim SelectedComboData As ComboData = TryCast(CType(sender, Image).DataContext, ComboData)
SelectedComboData.LightOn = Not SelectedComboData.LightOn
e.Handled = True
End Sub
LightOn值按照预期更改,即使在&#34; x&#34; (combodata列表),值改变了。但显示的图像没有变化。
我错过了什么?
谢谢!
答案 0 :(得分:0)
类comboData应实现INotifyPropertyChanged,以便通知UI有关其更改的内容。
Class ComboData
Implements INotifyPropertyChanged
Private _LightOn As Boolean
Public Property LightOn As Boolean
Get
Return _LightOn
End Get
Set(value As Boolean)
_LightOn = value
OnPropertyChanged("LightOn")
End Set
End Property
Private _name As String
Public Property name As String
Get
Return _name
End Get
Set(value As String)
_name = value
OnPropertyChanged("name")
End Set
End Property
Sub New(name_ As String, Light_On As Boolean)
_LightOn = Light_On
_name = name_
End Sub
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Protected Sub OnPropertyChanged(propertyName As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
End Class