Private Sub Button3_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button3.Click
'動的に、コンボボックに表示されている画像を変える。
'=動的にimageコントロールの画像をロードする
'"/WpfApplicationTips;component/Images/DSC07162.jpg"
'参考 http://msdn.microsoft.com/ja-jp/library/system.windows.media.imaging.bitmapimage.aspx
' Create source.
Dim bi As New BitmapImage()
' BitmapImage.UriSource must be in a BeginInit/EndInit block.
bi.BeginInit()
bi.UriSource = New Uri("/WpfApplicationTips;component/Images/DSC07162.jpg", UriKind.RelativeOrAbsolute)
bi.EndInit()
Image2.Source = bi
End Sub
選択した値を取得その1
<ComboBox Grid.Row="3" Height="24" HorizontalAlignment="Left" Name="ComboBox1" VerticalAlignment="Top" Width="120" Style="{StaticResource thisPG_Margin}" >
<ComboBoxItem IsSelected="True">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Content="北海道" />
<Image Grid.Column="1" Name="Image2" Stretch="Fill" VerticalAlignment="Top" Source="/WpfApplicationTips;component/Images/o0640048011914843315.jpg" />
</Grid>
</ComboBoxItem>
<ComboBoxItem Content="青森" />
<ComboBoxItem Content="秋田" />
<ComboBoxItem Content="岩手" />
</ComboBox>
Private Sub ComboBox1_SelectionChanged(sender As System.Object, e As System.Windows.Controls.SelectionChangedEventArgs) Handles ComboBox1.SelectionChanged
Try
If Me.ComboBox1.SelectedIndex <> -1 Then
Dim cbI As ComboBoxItem = Me.ComboBox1.SelectedItem
'MessageBox.Show(cbI.Content) ’テキストだとこれでよいが
Dim gd As Grid = cbI.Content
Dim ob As Object = gd.Children(1)
Dim img As Image = CType(ob, Image)
Debug.Print(img.Source.ToString)
'pack://application:,,,/WpfApplicationTips;component/Images/o0640048011914843315.jpg
'pack://application:,,,/WpfApplicationTips;component/Images/DSC07162.jpg
End If
Catch ex As Exception
MessageBox.Show(ex.Message.ToString)
End Try
End Sub
選択した値を取得その2
Private Sub ComboBox2_SelectionChanged(sender As System.Object, e As System.Windows.Controls.SelectionChangedEventArgs) Handles ComboBox2.SelectionChanged
'SelectedValuePathで指定したばあい。
If Me.ComboBox2.SelectedIndex <> -1 Then
MessageBox.Show(Me.ComboBox2.SelectedValue)
End If
End Sub
|