c# - 为 ComboBox 中的 ItemSource 值添加前缀

标签 c# wpf combobox itemsource

这是在我的 app.xaml 文件中(缩减):

<Application.Resources>
    <x:Array x:Key="SongTitleString" Type="local:ComboBoxItemString">
        <local:ComboBoxItemString ValueString = "Song 1"/>
        <local:ComboBoxItemString ValueString = "Song 2"/>
        <local:ComboBoxItemString ValueString = "Song 3"/>
    </x:Array>
</Application.Resources>

这是类:

namespace OCLMEditor
{
    /// This class provides us with an object to fill a ComboBox with
    /// that can be bound to string fields in the binding object.
    public class ComboBoxItemString
    {
        public string ValueString { get; set; }
    }
}

这是标记:

<ComboBox x:Name="comboSongOpen" ItemsSource="{StaticResource SongTitleString}" 
    DisplayMemberPath="ValueString" 
    SelectedValuePath="ValueString" 
    SelectedValue="{Binding SongTitle}">
    <ComboBox.MaxWidth>
        <Binding Path="ActualWidth" 
            ElementName="textWeeklyBibleReading"/>
    </ComboBox.MaxWidth>
</ComboBox>

一切顺利。这个问题是,当歌曲标题显示在组合上时,我想在歌曲标题前加上一个 3 位数字。例如:

001 - Song 1
002 - Song 2
003 - Song 3

最终,在应用程序的其他地方,我会想使用组合框选择的索引从资源中提取正确的歌曲标题(没有前缀)。

这可能吗?

更新:

如果我创建一个 XML:

<?xml version="1.0" encoding="utf-8" ?>
<Settings>
  <SongTitles>
    <SongTitle Number="1" Title = "Jehovah's Attributes"/>
  </SongTitles>
</Settings>

并将其作为资源添加到 WPF:

<Window.Resources>
    <XmlDataProvider x:Key="XmlData" Source="OCLM_Resources.xml" XPath="Settings/SongTitles" />
</Window.Resources>

然后使用:

<ComboBox x:Name="comboSongOpen"
           ItemsSource="{Binding Source={StaticResource XmlData}, XPath=./SongTitle}" DisplayMemberPath="@Title">
    <ComboBox.MaxWidth>
        <Binding Path="ActualWidth" 
            ElementName="textWeeklyBibleReading"/>
    </ComboBox.MaxWidth>
</ComboBox>

在下拉列表中显示标题。但我无法完全理解使用 ItemTemplate 和/或将属性(用于格式化 0.000)结合在一起。

最佳答案

这是一个非常简单的纯 xaml 解决方案。您无需更改任何其他内容。

            //add this to your resource.  Obviously you should put this in code if you have lots of items
            <AlternationConverter x:Key="AlternateForegroundConverter">
                <s:Int16>1</s:Int16>
                <s:Int16>2</s:Int16>
                <s:Int16>3</s:Int16>
                <s:Int16>4</s:Int16>
                <s:Int16>5</s:Int16>
                //more if necessary
            </AlternationConverter>

        <ComboBox x:Name="comboSongOpen" ItemsSource="{StaticResource SongTitleString}" SelectedValuePath="ValueString" SelectedValue="{Binding SongTitle}" AlternationCount="99999" >
            <ComboBox.ItemTemplate>
                <DataTemplate DataType="local:ComboBoxItemString">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding (ItemsControl.AlternationIndex), RelativeSource={RelativeSource AncestorType=ListBoxItem}, StringFormat='000 - ', Converter={StaticResource AlternateForegroundConverter}}" />
                        <TextBlock Text="{Binding ValueString}" />
                    </StackPanel>
                </DataTemplate>
            </ComboBox.ItemTemplate>
            <ComboBox.MaxWidth>
                <Binding Path="ActualWidth" ElementName="textWeeklyBibleReading"/>
            </ComboBox.MaxWidth>

编辑:需要这个命名空间

xmlns:s="clr-namespace:System;assembly=mscorlib"

编辑:回应更新的问题

将 DataTemplate 更改为(在这种情况下您不需要 AlternationConverter)。

        <ComboBox.ItemTemplate>
            <DataTemplate DataType="local:ComboBoxItemString">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Number, StringFormat='000 - '}" />
                    <TextBlock Text="{Binding ValueString}" />
                </StackPanel>
            </DataTemplate>

//add Number property to you class
public class ComboBoxItemString
{
    public string ValueString { get; set; }
    public int Number { get; set; }
}

//add the array
<x:Array x:Key="SongTitleString" Type="local:ComboBoxItemString">
    <local:ComboBoxItemString ValueString = "Song 1" Number = "1" />
    <local:ComboBoxItemString ValueString = "Song 2" Number = "2" />
    <local:ComboBoxItemString ValueString = "Song 3" Number = "3" />
</x:Array>

但是有一个很大的问题。你列出的是正确的。但是如果您选择一个项目,组合框也会显示“001 - Jehovah's Attributes”。要让它只显示“耶和华的属性”,需要修改控件模板。

关于c# - 为 ComboBox 中的 ItemSource 值添加前缀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37889219/

相关文章:

c# - 为什么以辅助角色托管 WCF 服务

c# - 为什么对同步请求使用 HttpClient 而不是 HttpWebRequest

c# - ComboBox 内容的自动宽度

c# - wpf 向导工具包 ItemsSource 绑定(bind)到列表

c# - 免费的高级下拉列表控件 (Windows Forms .Net)

java - 从 netbeans GUI 中的组合框获取值

c# - 我们可以在 C# 中为运行时创建的控件在运行时创建多个事件吗

c# - 通用列表 c# 中的重复值

c# - 如何关闭外部网络浏览器中的选项卡?

wpf - 当通过DataTemplate应用时,为什么没有代码隐藏我的WPF View 不起作用?