首页 > 解决方案 > 如何自定义 SfComboBox 的选中值显示模板

问题描述

我无法调整 a 的项目显示样式SfComboBox。组合框正在使用DataSource. 似乎给定的ItemTemplate仅影响选项列表,而不影响显示所选值的实际字段。

问题(之前选择了“Deutschland”选项,但显示的是“de”。我希望使用相同的字符串):

在此处输入图像描述

我各自的 Xaml 示例:

<border:SfBorder Style="{DynamicResource SfBorderStyle}" Margin="5" BorderColor="{StaticResource primaryTint}" CornerRadius="8">
                        <combobox:SfComboBox x:Name="selectCountry" Margin="20,5,0,0" Style="{StaticResource DefaultComboBoxStyle}"
                                    Watermark="{x:Static p:Resources.SelectCountry}" SelectionChanged="countrySelectionChanged"
                                    SelectedItem="{Binding SelectedCountry}" DataSource="{Binding Countries}">
                            <combobox:SfComboBox.ItemTemplate>
                                <DataTemplate>
                                    <Label Text="{Binding Name[de]}"/>
                                </DataTemplate>
                            </combobox:SfComboBox.ItemTemplate>
                        </combobox:SfComboBox>
                    </border:SfBorder>

数据对象的类(注意:生成此类。因此我无法修改类结构):

public class Country
    {
        [JsonProperty("code")]
        public string Code {get; set;}
    
        [JsonProperty("name")]
        public Dictionary<string, string> Name {get; set;}
    
        [JsonProperty("active")]
        public bool Active {get; set;}
    
        [JsonProperty("create_date")]
        public DateTime? CreateDate {get; set;}
    
        [JsonProperty("delete_date")]
        public DateTime? DeleteDate {get; set;}
    
        [JsonProperty("memo")]
        public string? Memo {get; set;}
    
        [JsonProperty("modify_date")]
        public DateTime? ModifyDate {get; set;}
    
    }

我的目标是组合框的字段使用与选项项相同的格式(例如,使用国家的德语名称)。如果可能的话,我想用普通的 XAML 来完成这个。我已经尝试过DisplayMemberPath了,combobox:SfComboBox没有成功。请记住:Country对象的Name属性是Dictionary.

标签: xamarin.formscomboboxsyncfusion

解决方案


我们已经分析了您的查询,目前我们在 SfComboBox 中不支持 DisplayMemberPath 的字典,我们已经记录了对此的功能请求,您可以从下面的链接跟踪功能的状态

反馈门户: https ://www.syncfusion.com/feedback/22399/provide-dictionary-datatype-support-for-displaymemberpath

请投下你的一票,让它发挥作用。我们将根据需求对每个版本的功能进行优先级排序,并且我们没有立即实施该功能的计划,因为我们已经承诺了已经计划好的工作。因此,此功能将在我们即将发布的任何版本中提供。


推荐阅读