【问题标题】:How to make ListBox editable when bound to a List<string>?绑定到 List<string> 时如何使 ListBox 可编辑?
【发布时间】:2011-08-13 04:49:02
【问题描述】:

编辑:基本问题是将 List 绑定到 ListBox(或任何其他控件)。所以我正在编辑问题。

我将一个字符串列表绑定到一个 ListBox,如下所示。但是,当我更改文本框的内容时,它并没有更改源列表中的字符串。为什么?

  public partial class MainWindow : Window
{
    List<string> _nameList = null;

    public List<string> NameList
    {
        get
        {
            if (_nameList == null)
            {
                _nameList = new List<string>();
            }
            return _nameList;
        }
        set
        {
            _nameList = value;
        }
    }
    public MainWindow()
    {
        NameList.Add("test1");
        NameList.Add("test2");
        InitializeComponent();
    }

还有 XAML

 <ListBox Grid.Row="0" Grid.Column="0" DataContext="{Binding ElementName=main}" ItemsSource="{Binding NameList}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBox Text="{Binding .,Mode=OneWayToSource ,  UpdateSourceTrigger=PropertyChanged}"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

【问题讨论】:

    标签: wpf data-binding listbox wpfdatagrid


    【解决方案1】:

    如果我没有误解你的问题,它很容易实现。看:

    <ComboBox Text="My Comment 5 with addition." IsEditable="True" Height="25" Width="200">
            <ComboBoxItem>My comment1</ComboBoxItem>
            <ComboBoxItem>My comment2</ComboBoxItem>
    </ComboBox>
    

    【讨论】:

    • 你懂的,他希望列表项是可编辑的。
    【解决方案2】:

    每个ListBoxItemDataContext 是字符串本身,因此您的绑定路径为空(.)。 TwoWayOneWayToSource 绑定需要路径,因为您不能只替换当前的 DataContext。因此,您需要将字符串包装在一个将字符串作为属性公开的对象中:

    public class StringItem
    {
        public string Value { get; set; }
    }
    

    将字符串公开为StringItem的列表:

    public partial class MainWindow : Window
    {
        List<StringItem> _nameList = null;
    
        public List<StringItem> NameList
        {
            get
            {
                if (_nameList == null)
                {
                    _nameList = new List<StringItem>();
                }
                return _nameList;
            }
            set
            {
                _nameList = value;
            }
        }
        public MainWindow()
        {
            NameList.Add(new StringItem { Value = "test1" });
            NameList.Add(new StringItem { Value = "test2" });
            InitializeComponent();
        }
    

    并绑定到Value 属性:

    <ListBox Grid.Row="0" Grid.Column="0" DataContext="{Binding ElementName=main}" ItemsSource="{Binding NameList}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBox Text="{Binding Value, UpdateSourceTrigger=PropertyChanged}"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    

    请注意,StringItem 还需要实现 INotifyPropertyChanged 以便自动更新绑定。您还应该将列表公开为 ObservableCollection&lt;T&gt; 而不是 List&lt;T&gt;

    【讨论】:

    • 谢谢托马斯,很好的回答。仅供参考,代码“public string class StringItem{}”让我感到困惑,可能是一个错误。
    • @Jimmy,是的,这是一个错误……我一定是在打字时分心了;)
    【解决方案3】:

    可能有帮助吗?

    <ListBox Name="lsbList">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBox Text="{Binding Path=Value}"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    

    【讨论】:

    • 您可以像这样编辑 UI,但编辑不会反映在列表对象中
    • 什么意思? NameColumnField - 您的对象列表。例如,它的属性之一具有名称值。值绑定到 DataTemplate 中的 TextBox。如果你在文本框中改变它,它不会在有界对象中改变?
    • 另外,请检查我归结为实际问题的已编辑问题。
    • +1,阅读托马斯的回答后,我明白你想说什么。+
    【解决方案4】:

    您可以创建一个带有项目控件和文本框的 DataGridTemplateColumn.CellEditingTemplate 来编辑您的项目

    【讨论】:

    • 您可以像这样编辑 UI,但编辑不会反映在列表对象中
    • 请检查已编辑的问题。即使我可以在 UI 中编辑文本框,它也不会反映在源代码中
    猜你喜欢
    • 2013-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-11
    • 2011-02-10
    • 1970-01-01
    相关资源
    最近更新 更多