我可以将文本块的内容仅绑定到可观察集合中字符串的一部分吗?

发布于 2024-11-29 17:44:56 字数 1497 浏览 2 评论 0原文

我对 WPF 比较陌生,因此如果我没有看到对此问题的明显或简单的答案,我深表歉意。

我有一个 ObservableCollection 项目,每个项目都有几个不同大小的图像。 每个图像的相对路径都是字符串格式,图像文件存储在不同的子文件夹中。

图像路径的格式如下:

imagepath = @"subfolder/subfolder/filename.png"

我希望能够绑定到文本块,但仅显示每个图像下方的文件名,而不更改图像路径。这可能吗?我认为它需要某种转换器,但我一直在努力解决这个问题,因为我找不到一种合理的方法来仅显示字符串的一部分。

感谢您的帮助。

编辑

澄清一下,我的“值”是非静态的,引用可观察集合中的项目,例如。

ObservableCollection<Icon> items = new ObservableCollection<Item>();

items.Add(new Item{imagename = "someimagename",
imagepath= "somefolder/somesubfolder/somefilename.png"}) etc...

我才刚刚开始掌握如何从我的收藏中“获取”价值。任何有关填写“value.Tostring()”部分以获取 item.imagepath 的动态值的帮助都将非常感激,以便我可以正常工作。

我目前一直在尝试这样做:

class getFilenameFromPathConverter : IValueConverter
 {     
    public object Convert(object value, Type targetType, object parameter,
    System.Globalization.CultureInfo culture)

      {    Item item = value as Item; 
           PropertyInfo info = value.GetType().GetProperty("imagepath");
           string filename = info.GetValue(item, null).ToString();
           return System.IO.Path.GetFileNameWithoutExtension(filename); } 

    public object ConvertBack(object value, Type targetType, object parameter,
       System.Globalization.CultureInfo culture)
       {
          throw new NotImplementedException();
        }
   }

但我收到了未处理的异常:System.NullReferenceException:未将对象引用设置为对象的实例。

谢谢

I'm relatively new to WPF so apologies if there is an obvious or simple answer to this that I am not seeing.

I have an ObservableCollection of items with several different sized images for each.
The relative path for each image is in string format, with image files stored in different subfolders.

The image paths are in the format:

imagepath = @"subfolder/subfolder/filename.png"

I would like to be able to bind to a textblock but show only the filename below each image without changing the image path. Is this possible? I gather it would require a converter of some sort, but I have been struggling with it as I cannot find a reasonable way of showing only part of a string.

Thanks for your help.

Edit

To clarify, my 'value' is non-static, referencing items in an observable collection eg.

ObservableCollection<Icon> items = new ObservableCollection<Item>();

items.Add(new Item{imagename = "someimagename",
imagepath= "somefolder/somesubfolder/somefilename.png"}) etc...

I'm only starting to get to grips with 'getting' values from my collection. Any help with filling in the 'value.Tostring()' part to get dynamic values for item.imagepath would be really appreciated so I can get this working.

I've currently been trying along these lines:

class getFilenameFromPathConverter : IValueConverter
 {     
    public object Convert(object value, Type targetType, object parameter,
    System.Globalization.CultureInfo culture)

      {    Item item = value as Item; 
           PropertyInfo info = value.GetType().GetProperty("imagepath");
           string filename = info.GetValue(item, null).ToString();
           return System.IO.Path.GetFileNameWithoutExtension(filename); } 

    public object ConvertBack(object value, Type targetType, object parameter,
       System.Globalization.CultureInfo culture)
       {
          throw new NotImplementedException();
        }
   }

But I'm getting an Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寂寞美少年 2024-12-06 17:44:56

由 fat 指定的 XAML。

您的转换器应如下所示:

class getFilenameFromPathConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return Path.GetFileName(value.ToString());
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

XAML as specified by fatty.

Your converter should look like:

class getFilenameFromPathConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return Path.GetFileName(value.ToString());
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
烟花易冷人易散 2024-12-06 17:44:56

您需要寻求 转换器 来获取此值。

创建一个实现 IValueConverter 的类,并在 Convert 方法中返回要显示的字符串部分。

在 TextBlock 中,绑定到诸如 {Binding Path=imagepath, Converter={StaticResource getFilenameFromPathConverter}} 的属性

You will need to enlist the help of a converter to get this value.

Create a class that implements IValueConverter, and in the Convert method return the part of the string that you want to display.

In your TextBlock, you bind to the property like {Binding Path=imagepath, Converter={StaticResource getFilenameFromPathConverter}}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文