【问题标题】:WPF TwoWay Binding of ListBox using DataTemplate [duplicate]使用DataTemplate的ListBox的WPF双向绑定[重复]
【发布时间】:2012-12-06 21:33:34
【问题描述】:

可能重复:
How to make ListBox editable when bound to a List<string>?

我正在尝试在名为“ListStr”的 List 对象和 ListBox WPF 控件之间设置两个绑定。 此外,我希望这些项目是可编辑的,所以我添加了一个带有 TextBoxes 的 DataTemplate,希望它可以通过 TextBoxes 直接修改 ListStr 项目。

但是当我尝试编辑其中一个时,它不起作用...

有什么想法吗?

PS:我已尝试添加 Mode=TwoWay 参数,但仍然无法正常工作

这是 XAML:

<ListBox ItemsSource="{Binding Path=ListStr}" Style="{DynamicResource ResourceKey=stlItemTextContentListBoxEdit}" />

这里是样式代码:

<Style x:Key="stlItemTextContentListBoxEdit" TargetType="{x:Type ListBox}">
<Setter Property="Background" Value="#FF0F2592" />
<Setter Property="BorderBrush" Value="Transparent" />
<Setter Property="Foreground" Value="White" />
<Setter Property="Height" Value="150" />
<Setter Property="Width" Value="200" />
<Setter Property="HorizontalContentAlignment" Value="Left" />
<Setter Property="ItemTemplate" Value="{DynamicResource ResourceKey=dtplItemTextContentListBoxEdit}" /></Style>

还有数据模板:

<DataTemplate x:Key="dtplItemTextContentListBoxEdit">
    <TextBox Text="{Binding Path=.}" Width="175" />
</DataTemplate>

【问题讨论】:

    标签: wpf xaml binding listbox datatemplate


    【解决方案1】:

    当您使用{Binding Path=.}{Binding} 很长)时,双向绑定不起作用。记住正在发生的事情。

    ListBox 得到一个对象列表,然后它为每个项目创建一个 ListBoxItem。然后将ListBoxItemDataContext 设置为该对象。当您使用{Binding} 时,您是说只使用DataContext 中的对象。当您在 TextBox 中键入内容时,它会更新什么?它无法设置 DataContext 并且不知道对象来自哪里(因此它无法更新您的列表/数组)。

    两种方式绑定起作用的地方是,当您绑定到该对象上的属性时。但不是当你绑定到对象本身时。

    【讨论】:

    • 我不确定:每个 ListBoxItem 的 DataContext 是什么?
    • 所以我的问题很简单:我想修改一个与 ListBox 绑定的 List。我该怎么做?
    • 你需要一个带有 setter 的项目,这样绑定会改变你的数据,创建一个带属性的类,并使用它插入字符串并绑定到属性
    • @Pamplemousse - 您将无法使用这样的 ListBox 修改 List。请参阅 ZSH 的答案以了解如何完成。
    【解决方案2】:
        public class ViewModel
    {
        ObservableCollection<TextItem> _listStr = new ObservableCollection<TextItem> { new TextItem("a"), new TextItem("b"), new TextItem("c") };
    
        public ObservableCollection<TextItem> ListStr
        {
            get  { return _listStr; }  // Add observable and propertyChanged here also if needed
        }
    }
    
    public class TextItem : NotificationObject // (NotificationObject from Prism you can just implement INotifyPropertyChanged)
    {
        public TextItem(string text)
        {
            Text = text;
        }
    
        private string _text;
        public string Text
        {
            get { return _text; }
            set
            {
                if (_text != value)
                {
                    _text = value;
                    RaisePropertyChanged(() => Text);
                }
            }
        }
    }
    


    xaml:

     <DataTemplate>
        <TextBox Text="{Binding Text}" Width="175" />
     </DataTemplate>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-07
      • 2011-01-27
      • 1970-01-01
      • 2010-09-24
      • 2011-07-13
      • 2010-11-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多