[英]C# ListBox Update Binding Text
I have a ListBox
on WP8.1 and want to Bind some items in there. 我在WP8.1上有一个
ListBox
,想在其中绑定一些项目。 That works all fine, but changing a value on the ItemSource
doesn't change anything in the ListBox
一切正常,但是更改
ItemSource
的值不会更改ListBox
任何内容
<ListBox x:Name="myListBox" Width="Auto" HorizontalAlignment="Stretch" Background="{x:Null}" Foreground="{x:Null}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel x:Name="PanelTap" Tapped="PanelTap_Tapped">
<Border x:Name="BorderCollapsed">
<StackPanel Margin="105,0,0,0">
<TextBlock Text="{Binding myItem.location, Mode=TwoWay}" />
</StackPanel>
</Border>
</ListBox.ItemTemplate>
</ListBox>
I bind the items via 我通过以下方式绑定项目
ObservableCollection<LBItemStruct> AllMyItems = new ObservableCollection<LBItemStruct>();
with 同
public sealed class LBItemStruct
{
public bool ext { get; set; }
public Container myItem { get; set; }
}
public sealed class Container
{
public string location{ get; set; }
...
}
and when I now want to change the TextBlock
Text, nothing happens 当我现在想更改
TextBlock
Text时,什么也没发生
private void PanelTap_Tapped(object sender, TappedRoutedEventArgs e)
{
int sel = myListBox.SelectedIndex;
if (sel >= 0)
{
myListBox[sel].myItem.location = "sonst wo";
}
}
The PanelTap_Tapped
gets triggered, when I tap the Panel (checked via Debug), but the TextBlock Text does not change 当我点击面板(通过Debug选中)时,
PanelTap_Tapped
被触发,但是TextBlock文本不会改变
If you want the view to update when a property changes, then you need to have the source object implement INotifyPropertyChaned
, and raise the PropertyChanged
event: 如果要在属性更改时更新视图,则需要使源对象实现
INotifyPropertyChaned
并引发PropertyChanged
事件:
public sealed class Container : INotifyPropertyChanged
{
public string location
{
get { return _location; }
set { _location = value; RaisePropertyChanged("location"); }
}
private string _location;
...
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propName)
{
var handler = PropertyChanged;
if (handler != null)
handler(new PropertyChangedEventArgs(this, propName));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.