我在修改包含UserControl集合的ListBox时遇到问题。我想更新一个特定的UserControl项,但是当绑定类发生更改时它没有被更新。
班级代码:
private ObservableCollection<User> _UserList;
public ObservableCollection<User> UserList
{
get
{
return _UserList;
}
set
{
_UserList = value;
NotifyPropertyChanged();
}
}
public void EditUser()
{
User wat = UserList.FirstOrDefault(ul => ul.Handle == "JaviSRK");
wat.Color = "Red";
NotifyPropertyChanged("UserList");
}
XAML:
<ListBox Margin="10,0,0,0" Name="lstLocalUsers" Height="510" Width="225"
HorizontalAlignment="Left" ItemsSource="{Binding UserList}" SelectionMode="Multiple">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<tplayer:UserControl />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
UserControl XAML:
<Grid>
<StackPanel Orientation="Vertical" >
<StackPanel Orientation="Horizontal">
<TextBlock Foreground="{Binding Color}" Name="txtLastName" Text="{Binding LastName}" FontWeight="Bold" />
<TextBlock Foreground="{Binding Color}" Text=", " />
<TextBlock Foreground="{Binding Color}" Name="txtFirstName" Text="{Binding FirstName}" FontWeight="Bold" />
<TextBlock Foreground="{Binding Color}" Name="txtHandle" Text="{Binding Handle, StringFormat=({0})}" FontWeight="Bold" Margin="5,0,0,0" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Foreground="{Binding Color}" Name="txtCity" Text="{Binding City}" />
<TextBlock Foreground="{Binding Color}" Text=", " />
<TextBlock Foreground="{Binding Color}" Name="txtState" Text="{Binding State}" />
</StackPanel>
</StackPanel>
</Grid>
UserList集合正确更新,但UserControl本身没有。我该怎么做?
答案 0 :(得分:0)
事实证明,User类本身需要实现INotifyPropertyChanged接口。
public class User : INotifyPropertyChanged
{
public string _Color;
public string Color
{
get
{
return _Color;
}
set
{
_Color = value;
NotifyPropertyChanged();
}
}
}