gpt4 book ai didi

c# - WPF:将 ComboBox 数据绑定(bind)到 int 值

转载 作者:太空狗 更新时间:2023-10-30 01:36:23 24 4
gpt4 key购买 nike

我有这样一个类:

public class Service
{
public int State { get; set; }
}

如何将它绑定(bind)到 ComboBox,以便组合显示“Active”/“Inactive”并且状态变为 1 或 0?

<ComboBox SelectedItem="{Binding Path=State}">
<ComboBoxItem Content="Active" Tag="1" IsSelected="True"/>
<ComboBoxItem Content="Inactive" Tag="0" />
</ComboBox>

我正在使用 DataContext form.DataContext = new Service();

将对象绑定(bind)到表单

最佳答案

首先,Tag 属性只包含您提供的任何数据,它对 SelectedItem 的绑定(bind)没有任何贡献。如果 ComboBoxItems 没有数据上下文,您也许可以这样做,试一试:

<ComboBox SelectedValue="{Binding Path=State}" SelectedValuePath="Tag">
<ComboBoxItem Content="Active" Tag="1" IsSelected="True"/>
<ComboBoxItem Content="Inactive" Tag="0" />
</ComboBox>

关于c# - WPF:将 ComboBox 数据绑定(bind)到 int 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22360034/

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