簡體   English   中英

如何在運行時從選定的ListBox項目將Image源設置為空白Image控件?

[英]How to set an Image source to a blank Image control at runtime from selected ListBox Item?

問題:

我有一個名為“ Image1 ”的空白Image控件。

現在,我想在運行時根據列表框(ListBoxSource)上的選定項目為該圖像提供源。

如何在“ ListBoxSource_SelectionChanged(...)”事件中執行此操作?

 private void ListBoxSource_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
    {
    //...Some conditional statement in here which determines what image to set or update its Image source

        this.Image1.Source = ??? What to supply in here

    }

注意:我知道如何使用綁定按鈕做到這一點, 但我只知道使用另一個ListBox顯示我選擇的項目。 這次我使用Grid(2行2列:每個單元格中都有一個空白的Image控件),目的是為每個網格單元格提供ListBoxSource選定的Image項。

或者可以使用Grid對此進行一些綁定嗎? 我的另一個目的是能夠控制不同大小的圖像(這意味着某些圖像將跨越網格內的行/列)。

這是我的XAML:

 <Window.Resources>
    <DataTemplate x:Key="ItemTemplate">
        <StackPanel>
            <Image Source="{Binding FileFullName}" HorizontalAlignment="Left" Height="64" Width="64"/>
            <TextBlock Text="{Binding FileName}"/>
        </StackPanel>
    </DataTemplate>
</Window.Resources>

<Grid x:Name="LayoutRoot" DataContext="{Binding Source={StaticResource SampleDataSource}}">
    <ListBox x:Name="ListBoxSource" HorizontalAlignment="Left" ItemTemplate="{DynamicResource ItemTemplate}" ItemsSource="{Binding Collection}" Margin="29,31,0,31" Width="257" SelectionMode="Multiple" SelectionChanged="listBoxSource_SelectionChanged"/>
    <Grid x:Name="GridImageHolder" Height="270" Margin="338,44,0,0" VerticalAlignment="Top" Background="#FFE0D6D6" ShowGridLines="True" DataContext="{Binding SelectedItem, ElementName=listBoxSource}" d:DataContext="{Binding Collection[0]}" HorizontalAlignment="Left" Width="539">
        <Grid.RowDefinitions>
            <RowDefinition Height="130"/>
            <RowDefinition Height="140"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="267.883"/>
            <ColumnDefinition Width="271.117"/>
        </Grid.ColumnDefinitions>
        <Image x:Name="Image1" Grid.Row="0" Grid.Column="0" Margin="8,0.96,21.883,8"/>
        <Image x:Name="Image2" Grid.Row="0" Grid.Column="1" HorizontalAlignment="Left" Margin="19.975,0,0,8" Width="218"/>
        <Image x:Name="Image3" Grid.Row="1" Grid.Column="0" Margin="8,21.04,40.883,16"/>
        <Image x:Name="Image4" Grid.Row="1" Grid.Column="1" Margin="8,21.04,33.117,16" />

    </Grid>

</Grid>

在這種情況下,您需要將發送方轉換為適當的類。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM