C# 更改列表框中项目的外观而不更改/获取原始SelectedItem值

C# 更改列表框中项目的外观而不更改/获取原始SelectedItem值,c#,wpf,xaml,listbox,C#,Wpf,Xaml,Listbox,我正在以这种方式绑定属性上的列表框。设置StringCollection <ListBox Height="52" HorizontalAlignment="Left" Margin="12,0,0,148" Name="FolderList" VerticalAlignment="Bottom" Width="120" ItemsSource="{Binding Source={x:Static properties:Settings.Default},Path=Folders}"/&

我正在以这种方式绑定
属性上的
列表框
。设置
StringCollection

 <ListBox Height="52" HorizontalAlignment="Left" Margin="12,0,0,148" Name="FolderList" VerticalAlignment="Bottom" Width="120" ItemsSource="{Binding Source={x:Static properties:Settings.Default},Path=Folders}"/>
它起作用了。我的问题是:


当我执行button\u click事件以获取SelectedItem时,获取原始(未转换)字符串的干净方法是什么?我可能可以直接访问属性、计算索引和比较,但可能有一种更干净的方法。

有一种更简单的方法,无需编写自定义转换器来去掉1个字符。看看我拼凑的代码。这是seudo代码,但它应该会给你一个想法

FolderObject
{
    private string _name;
    public string name 
    { 
        get { return this._name; } 
        set
        {
            this._name = value.trim( new char[]{ '$' } )
        }
    }

    private string _raw;
    public string raw 
    { 
        get { return this._raw; }  
        set { this._raw = value; }
    }
}

class form1 : Form
{
    private List<FolderObject> _folders;
    private BindingSource _bindingSource;

    public form1()
    {
        Initialize();
    }

    public void Initialize()
    {
        _folders = new List<FolderObject>();
        _bindingSource = new BindingSource();

        List<FolderObject> folders = new List<FolderObject>();
        //Fill folders ...

        _bindingSource.DataSource = folders;

        //bind collection to listbox
        listbox1.DisplayMember = "Name";
        listbox1.ValueMember = "Raw";
        listbox1.DataSource = _bindingSource;
    }

    button1_Click(object sender, System.EventArgs e)
    {
        Console.WriteLine(string.format("Folder name is: {0}, Raw name is: {1}", listbox1.SelectedMember, listbox1.SelectedValue));
    }
}
FolderObject
{
私有字符串\u名称;
公共字符串名
{ 
获取{返回此。_name;}
设置
{
此._name=value.trim(新字符[]{'$'})
}
}
私有字符串_raw;
公共字符串原始
{ 
获取{返回此。_raw;}
设置{this.\u raw=value;}
}
}
类别表格1:表格
{
私人列表文件夹;
私有BindingSource\u BindingSource;
公共表格1()
{
初始化();
}
公共无效初始化()
{
_文件夹=新列表();
_bindingSource=新的bindingSource();
列表文件夹=新列表();
//填充文件夹。。。
_bindingSource.DataSource=文件夹;
//将集合绑定到列表框
listbox1.DisplayMember=“Name”;
listbox1.ValueMember=“原始”;
listbox1.DataSource=\u bindingSource;
}
按钮1\u单击(对象发送者,System.EventArgs e)
{
WriteLine(string.format(“文件夹名称为:{0},原始名称为:{1}”,listbox1.SelectedMember,listbox1.SelectedValue));
}
}

谢谢,如果没有更多答案,我会调查的。但我更愿意找到一种方法,将我的XAML绑定到属性,而不是在运行时绑定它,因为这些值在执行期间不会重新填充。我还实现了转换器,认为我可以使用ConvertBack做我想做的事情,但我认为我不能做任何与之相关的事情
FolderObject
{
    private string _name;
    public string name 
    { 
        get { return this._name; } 
        set
        {
            this._name = value.trim( new char[]{ '$' } )
        }
    }

    private string _raw;
    public string raw 
    { 
        get { return this._raw; }  
        set { this._raw = value; }
    }
}

class form1 : Form
{
    private List<FolderObject> _folders;
    private BindingSource _bindingSource;

    public form1()
    {
        Initialize();
    }

    public void Initialize()
    {
        _folders = new List<FolderObject>();
        _bindingSource = new BindingSource();

        List<FolderObject> folders = new List<FolderObject>();
        //Fill folders ...

        _bindingSource.DataSource = folders;

        //bind collection to listbox
        listbox1.DisplayMember = "Name";
        listbox1.ValueMember = "Raw";
        listbox1.DataSource = _bindingSource;
    }

    button1_Click(object sender, System.EventArgs e)
    {
        Console.WriteLine(string.format("Folder name is: {0}, Raw name is: {1}", listbox1.SelectedMember, listbox1.SelectedValue));
    }
}