绑定到 List时如何使 ListBox 可编辑?

发布于 2024-11-03 14:03:35 字数 1097 浏览 4 评论 0原文

编辑:基本问题是将列表绑定到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>

Edit: The basic problem is binding a List to ListBox(or any other control). So I am editing the question.

I bound a list of string to a ListBox as below. However when I change the contents of the textbox it is not changing the string in the source list.Why?

  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();
    }

And the 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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

无人问我粥可暖 2024-11-10 14:03:35

每个 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 而不是 List

The DataContext of each ListBoxItem is the string itself, so the path of your binding is empty (.). TwoWay and OneWayToSource bindings require a path, since you can't just replace the current DataContext. So you need to wrap your string in an object that exposes the string as a property:

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

Expose the strings as a list of 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();
    }

And bind to the Value property:

<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>

Note that StringItem will also need to implement INotifyPropertyChanged so that bindings are automatically updated. You should also expose the list as an ObservableCollection<T> rather than a List<T>

可是我不能没有你 2024-11-10 14:03:35

可能有帮助吗?

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

May be it helsp?

<ListBox Name="lsbList">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBox Text="{Binding Path=Value}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
箜明 2024-11-10 14:03:35

您可以使用项目控件和文本框创建 DataGridTemplateColumn.CellEditingTemplate 来编辑项目

you can create a DataGridTemplateColumn.CellEditingTemplate with an itemscontrol and textboxes to edit your items

眼泪都笑了 2024-11-10 14:03:35

如果我没有误解你的问题,那么实现起来非常容易。看:

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

If I didn't misunderstand your question, it is pretty easy to implement. Look:

<ComboBox Text="My Comment 5 with addition." IsEditable="True" Height="25" Width="200">
        <ComboBoxItem>My comment1</ComboBoxItem>
        <ComboBoxItem>My comment2</ComboBoxItem>
</ComboBox>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文