gpt4 book ai didi

c# - 是否可以将数据绑定(bind)到枚举,并显示用户友好的值?

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

<分区>

我想显示我的契约(Contract)的状态,两者声明如下:

public enum RentStatus
{
[Description("Preparation description")]
Preparation,
[Description("Active description")]
Active,
[Description("Rented to people")]
Rented
}

public class RentContract
{
public int RentContractId { get; set; }
public virtual Premise Premise { get; set; }
public double Price { get; set; }
public RentStatus Status { get; set; }
}

我当前的 XAML 是错误的

<ComboBox x:Name="RentStatusComboBox"
ItemsSource="{Binding RentContract}"
Grid.Row="2"
Grid.Column="1"
HorizontalAlignment="Stretch"
SelectedItem="{Binding RentContract.Status}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Description}" Padding="0" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>

我见过几种使用转换器和方法的解决方案,但我认为它们无法允许数据绑定(bind),因此当我更改 UI 中的状态时我的实体也会更新。

编辑:关注this excellent blogpost解决了我的问题。

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