2011-10-24 19 views
1

私はカスタムアイテムを持つリストボックスを持っています。コード:リストボックスのアイテムはバインディングwp7で更新されません

<ListBox Height="600" HorizontalAlignment="Left" Margin="7,6,0,0" Name="friendList" VerticalAlignment="Top" Width="449" ItemsSource="{Binding Friends}"> 
      <ListBox.ItemsPanel> 
       <ItemsPanelTemplate> 
        <toolkit:WrapPanel /> 
       </ItemsPanelTemplate> 
      </ListBox.ItemsPanel> 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <Grid Margin="5,0"> 
         <Image Height="120" HorizontalAlignment="Left" Name="image" Stretch="Fill" VerticalAlignment="Top" Width="120" Source="{Binding ImageUri}" GotFocus="image_GotFocus"/> 
         <CheckBox Height="78" HorizontalAlignment="Left" Margin="65,63,0,0" x:Name="selectedChckbox" VerticalAlignment="Top" Width="55" IsChecked="{Binding Selected, Mode=TwoWay}"/> 
         <TextBlock Height="58" HorizontalAlignment="Left" Margin="0,122,0,0" x:Name="nameTextBlck" VerticalAlignment="Top" Text ="{Binding Title}" Width="120" TextWrapping="Wrap" GotFocus="name_GotFocus"/> 
        </Grid> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 

私は結合のための値のveiwmodelを作成しました、と私は1つのアイテムをクリックしたとき、私はそうのようなチェックボックスの状態を変更したい:

friendSelectionViewModel.Friends[_selectFriendContent.friendList.SelectedIndex].Selected = !friendSelectionViewModel.Friends[_selectFriendContent.friendList.SelectedIndex].Selected; 

のViewModelコード:

public class FacebookFriendSelectionViewModel : INotifyPropertyChanged 
{ 
    public FacebookFriendSelectionViewModel() 
    { 
     Friends = new ObservableCollection<TempFriends>(); 
    } 
     /// <summary> 
    /// A collection for MenuItemViewModel objects. 
    /// </summary> 
    public ObservableCollection<TempFriends> Friends { get; private set; } 

    public void AddItem(TempFriends item) 
    { 
     Friends.Add(item); 
    } 
    public event PropertyChangedEventHandler PropertyChanged; 
    private void NotifyPropertyChanged(string propertyName) 
    { 
     PropertyChangedEventHandler handler = PropertyChanged; 
     if (null != handler) 
     { 
      handler(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
} 

}パブリッククラスTempFriends {BOOLは_selected。

public string Title { get; set; } 

public string ImageUri { get; set; } 

public bool Selected { 
    get 
    { 
     return _selected; 
    } 
    set 
    { 
     _selected = value; 
     OnPropertyChanged("Selected"); 
    } 
} 

public string Id { get; set; } 


public TempFriends(string title, string imageUir, bool selected, string id) 
{ 
    Title = title; 
    ImageUri = imageUir; 
    _selected = Selected = selected; 
    Id = id; 
} 

public event PropertyChangedEventHandler PropertyChanged; 

private void OnPropertyChanged(String info) 
{ 
    if (PropertyChanged != null) 
    { 
     PropertyChanged(this, new PropertyChangedEventArgs(info)); 
    } 
} 

しかし、リストボックスには、私がnullにのDataContextを設定し、そのようaggainのviewmodelを割り当てるよりも、場合値は、更新を取得する唯一の方法:

_selectFriendContent.DataContext = null; 
    _selectFriendContent.DataContext = friendSelectionViewModel; 

しかし、これは、約5〜10秒かかりますリストをリフレッシュしてください。私は良い解決策があることを知っている、私はちょうど方法を把握できない。

ありがとうございます!

+0

"Selected"プロパティは、セッターでOnPropertyChanged( "Selected")を上げますか? – Nagg

答えて

3

TempFriendsクラスは、私が見るところではINotifyPropertyChangedを実装していません。パブリッククラスのTempFriendsを追加するだけです:INotifyPropertyChanged

+0

それはそれでした。ありがとう:) –

関連する問題