我只是想將組合框連接到影像。這意味著當我在組合框中選擇任何選項時,組合框會向我顯示該選項的影像。請幫我知道如何做到這一點?
uj5u.com熱心網友回復:
實作此行為的最簡單方法是在ComboBox.SelectedItem更改時通過處理ComboBox.SelectionChanged 事件來更改影像源。
這是它如何作業的簡單演示。首先,創建一個ComboBox控制元件并為其提供一些有關位置的資料。然后在后面的代碼中處理 ComboBox.SelectionChanged 事件并獲取所選專案。現在您可以檢查選定的值并更改影像的來源。
XAML:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<ComboBox x:Name="LocationComboBox" Header="Location" Height="60" Width="296" ItemsSource="{x:Bind source}" SelectionChanged="LocationComboBox_SelectionChanged">
<ComboBox.ItemTemplate>
<DataTemplate x:DataType="local:LocationData">
<Grid>
<TextBlock Text="{x:Bind location}"/>
</Grid>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<Image x:Name="LocationImage" Grid.Row="1"/>
</Grid>
代碼隱藏:
public sealed partial class MainPage : Page
{
public List<LocationData> source { get; set; }
public MainPage()
{
this.InitializeComponent();
source = new List<LocationData>();
source.Add(new LocationData { location = "london" });
source.Add(new LocationData { location = "paris" });
source.Add(new LocationData { location = "seattle" });
source.Add(new LocationData { location = "vancouver" });
}
private void LocationComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
LocationData data = LocationComboBox.SelectedItem as LocationData;
switch (data.location)
{
case "london":
LocationImage.Source = new BitmapImage(new Uri("ms-appx:///Assets/london.png"));
break;
case "paris":
LocationImage.Source = new BitmapImage(new Uri("ms-appx:///Assets/paris.png"));
break;
case "seattle":
LocationImage.Source = new BitmapImage(new Uri("ms-appx:///Assets/seattle.png"));
break;
case "Rvancouvered":
LocationImage.Source = new BitmapImage(new Uri("ms-appx:///Assets/vancouver.png"));
break;
}
}
}
public class LocationData
{
public string location { get; set; }
}
您可以根據自己的需求進行更改。
uj5u.com熱心網友回復:
為了將組合框與影像連接起來,我遵循了這些步驟——
XAML:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0">
<ComboBox x:Name="imageViewer" Width="200" Header="Picture" PlaceholderText="LifePicture" SelectionChanged="imageViewer_SelectionChanged" SelectedItem="{Binding Source, Mode=OneWay}" >
<x:String>America</x:String>
<x:String>Bombey</x:String>
<x:String>China</x:String>
<x:String>Delhi</x:String>
</ComboBox>
<Image x:Name="PictureView" Height="150" Width="150"
Margin="0,8,0,0" HorizontalAlignment="Left" />
</StackPanel>
</Grid>
代碼隱藏:
private void imageViewer_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string imageName = imageViewer.SelectedItem.ToString();
if(imageName == "America")
{
PictureView.Source = new BitmapImage(new Uri("ms-appx:///Assets/a.jpg"));
}
else if (imageName == "Bombey")
{
PictureView.Source = new BitmapImage(new Uri("ms-appx:///Assets/b.jpg"));
}
else if (imageName == "China")
{
PictureView.Source = new BitmapImage(new Uri("ms-appx:///Assets/c.jpg"));
}
else if (imageName == "Delhi")
{
PictureView.Source = new BitmapImage(new Uri("ms-appx:///Assets/d.jpg"));
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/358455.html
