使用的DataTemplate列表框的WPF双向绑定 [英] WPF TwoWay Binding of ListBox using DataTemplate
问题描述
可能重复:结果
<一href=\"http://stackoverflow.com/questions/5793438/how-to-make-listbox-editable-when-bound-to-a-liststring\">How使列表框可编辑在绑定到一个List&LT;串GT ;?
块引用>我想设置一个双结合命名为ListStr对象的列表和一个列表框WPF控件之间。
此外,我希望的项目是可编辑的,所以我加了一个DataTemplate与文本框期待它会通过文本框修改ListStr项目直线距离。但是,当我试图修改其中的一个,这是行不通的......
任何想法?
PS:我试图添加模式=双向参数,但它仍然没有工作。
下面是XAML:
&LT; ListBox中的ItemsSource ={绑定路径= ListStr}风格={DynamicResource的ResourceKey = stlItemTextContentListBoxEdit}/&GT;
下面是样式code:
&LT;风格X:键=stlItemTextContentListBoxEdit的TargetType ={X:类型列表框}&GT;
&LT; setter属性=背景VALUE =#FF0F2592/&GT;
&LT; setter属性=BorderBrushVALUE =透明/&GT;
&LT; setter属性=前景VALUE =白/&GT;
&LT; setter属性=高度值=150/&GT;
&LT; setter属性=宽度值=200/&GT;
&LT; setter属性=HorizontalContentAlignmentVALUE =左/&GT;
&LT; setter属性=ItemTemplate中VALUE ={DynamicResource的ResourceKey = dtplItemTextContentListBoxEdit}/&GT;&LT; /样式和GT;和中的DataTemplate:
&LT;的DataTemplate X:键=dtplItemTextContentListBoxEdit&GT;
&LT;文本框的文本={绑定路径=}WIDTH =175/&GT;
&LT; / DataTemplate中&GT;
解决方案当您使用
双向绑定不起作用{结合路径=}
(这是长{结合}
)。请记住发生了什么。的
的ListBox
给定对象的列表,它然后为每个项目创建一个ListBoxItem的
。在的DataContext
的ListBoxItem的
然后被设置到该对象。当您使用{结合}
,你说只使用了的DataContext
的对象。当你在文本框输入,那会是什么更新?它不能设置DataContext的,它有不知道那里的对象是从哪里来的(所以它不能更新列表/数组)。在哪里双向绑定没有工作,是当你绑定到该对象的属性。但不是在绑定到对象本身。
Possible Duplicate:
How to make ListBox editable when bound to a List<string>?I'm trying to set a two-binding between a List named "ListStr" object and a ListBox WPF control. Besides I want the items to be editable, so I added a DataTemplate with TextBoxes expecting that it would modify the ListStr items straight away via TextBoxes.
But when I'm attempting to edit one of them, it doesn't work...
Any Idea ?
PS: I've tried to add the Mode=TwoWay parameter, but it's still not working
Here is the XAML :
<ListBox ItemsSource="{Binding Path=ListStr}" Style="{DynamicResource ResourceKey=stlItemTextContentListBoxEdit}" />
Here is the style code :
<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>
And the DataTemplate:
<DataTemplate x:Key="dtplItemTextContentListBoxEdit"> <TextBox Text="{Binding Path=.}" Width="175" /> </DataTemplate>
解决方案Two way binding does not work when you use
{Binding Path=.}
(which is long for{Binding}
). Keep in mind what is happening.The
ListBox
is given a list of objects, which it then creates oneListBoxItem
for each item. TheDataContext
of theListBoxItem
is then set to that object. When you use{Binding}
, you are saying to just use the object in theDataContext
. When you type in the TextBox, what would it update? It can't set the DataContext and it has no idea where the object came from (so it can't update your list/array).Where two way binding does work, is when you bind to a property on that object. But not when you bind to the object itself.
这篇关于使用的DataTemplate列表框的WPF双向绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!