gpt4 book ai didi

c# - 绑定(bind)到绑定(bind)元素?

转载 作者:太空宇宙 更新时间:2023-11-03 21:13:34 25 4
gpt4 key购买 nike

我有 Comments 类,我要绑定(bind)到它:

<ListBox ItemsSource="{Binding CommentFiles}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Text}" TextWrapping="Wrap"/>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding UserId}"/> <!-- Here should be username -->
<TextBlock Text=","/>
<TextBlock Text="{Binding CreatedAt}"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

如您所见,Comments 类具有 UserId 属性,它只是一些字符组合。我可以使用异步 getUser(userID) 方法获取 User 类对象。
当我绑定(bind)到评论时,我想查看用户的用户名(在 User 类中)而不是 UserId。
而且我无法编辑类(class)。有办法做到这一点吗?

最佳答案

您可以将 userId 与采用 userId 的值转换器结合使用,调用 getUser(value) 并返回用户名。

<TextBlock Text="{Binding UserId, Converter={StaticResource MyUserIdConverter}" />

值转换器看起来像:

public class MyUserIdConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
// Add some checks here ;-)
return GetUser((string) value);
}

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

关于c# - 绑定(bind)到绑定(bind)元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36033839/

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