gpt4 book ai didi

c# - 从后面的代码中的列表框中获取 SelectedItem

转载 作者:太空宇宙 更新时间:2023-11-03 14:33:26 24 4
gpt4 key购买 nike

我有一个列表框,其中填充了来自 ImageDomainService(RIA 服务)的列表。我想从 ListBox 中选择一张图像并在其旁边显示该图像的较大版本。图像单独存储在/images/文件夹中如何将 ImageName 从 ListBox 绑定(bind)到代码后面的 url 字符串,如下所示?

void AlbumView_Loaded(object sender, RoutedEventArgs e)
{
ImageDomainContext ctx = new ImageDomainContext();
listBoxImages.ItemsSource = ctx.Images;
ctx.Load(ctx.GetImagesListQuery());
}

XAML:

<ListBox x:Name="listBoxImages" ItemsSource="{Binding}"
SelectionChanged="ListBox_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock x:Name="ImageNameTextBox" Text="{Binding ImageName}" />
<TextBlock Text="{Binding ImageDescription}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

事件处理器:

private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Image _image = new Image();
BitmapImage bi = new BitmapImage();

// string url = ???????????
bi.UriSource = new Uri(string.Format("images/{0}", url), UriKind.Relative);

_image.Source = bi;

_image.Width = 500;
_image.Height = 300;

bigImageBorder.Child = _image;
}

最佳答案

为什么不直接使用 SelectedItem属性代替?:

// Put the class that you're binding to here...
MyClass instance = listBoxImages.SelectedItem as MyClass;
string url = instance.ImageName; // url is an odd variable name for this...
bi.UriSource = new Uri(string.Format("images/{0}", url), UriKind.Relative);

此外,您还可以为直接执行此操作的所选项目创建一个 IValueConverter,这样您就可以将其他图像源直接绑定(bind)到所选项目,而无需任何代码隐藏。

关于c# - 从后面的代码中的列表框中获取 SelectedItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2061620/

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