2015-06-04 8 views
5

Desidero visualizzare diversi nomi e desidero che siano modificabili. Quindi ho usato una ObservableColection e l'ho associato a un ListView con la nuova funzione x: Bind.ListView binding bidirezionale compilato (x: Bind)

Ecco la mia XAML:

<ListView> 
     <ListView ItemsSource="{x:Bind ViewModel.Players}"> 
       <ListView.ItemContainerStyle> 
      <Style TargetType="ListViewItem"> 
       <Setter Property="HorizontalContentAlignment" Value="Stretch" /> 
      </Style> 
     </ListView.ItemContainerStyle> 

     <ListView.ItemTemplate> 
      <DataTemplate xmlns:model="using:Flechette.Model" x:DataType="model:Player"> 
       <TextBox Text="{x:Bind Name, Mode=TwoWay}" /> 
      </DataTemplate> 
     </ListView.ItemTemplate> 
    </ListView> 

E il mio codice dietro:

public sealed partial class GameSettingsPage : Page 
{ 
    ViewModel.GameSettingsViewModel ViewModel { get; set; } 

    public GameSettingsPage() 
    { 
     InitializeComponent(); 
     DataContextChanged += (s, e) => ViewModel = DataContext as ViewModel.GameSettingsViewModel; 
    } 
} 

Il problema è che TwoWay binding non è riuscito a compilare, ottengo 'WeakReference' l'errore CS1061 non contiene un definizione per 'LostFocus' e nessun metodo di estensione 'LostFocus' che accetta un primo argomento di tipo 'WeakReference' potrebbe essere trovato (ti manca una direttiva using o un riferimento assembly?)

Come posso risolvere il problema?

+0

si può mostrare il pieno XAML. Stai facendo x: legatura all'evento LostFocus? –

+0

[Il codice XAML completo] (http://pastebin.com/GL4vZn38) Come puoi vedere, voglio modificare gli elementi in ListView. Funziona correttamente con il binding classico, ma non con x: Bind – GaaH

+0

Se si modifica il binding TwoWay in OneWay, si verifica il problema? –

risposta

0

Questo sembra un problema nella versione di anteprima di Windows 10 SDK. Dato il seguente codice:

MainPage.xaml:

 <ListView x:Name="Players"> 
     <ListView.ItemContainerStyle> 
      <Style TargetType="ListViewItem"> 
       <Setter Property="HorizontalContentAlignment" Value="Stretch" /> 
      </Style> 
     </ListView.ItemContainerStyle> 

     <ListView.ItemTemplate> 
      <DataTemplate x:DataType="local:Player"> 
       <TextBox Text="{x:Bind Name, Mode=TwoWay}" /> 
      </DataTemplate> 
     </ListView.ItemTemplate> 
    </ListView> 

MainPage.xaml.cs:

private ObservableCollection<Player> players = new ObservableCollection<Player>(); 
    public MainPage() 
    { 
     this.InitializeComponent(); 
    } 

    protected override void OnNavigatedTo(NavigationEventArgs e) 
    { 
     this.players.Add(new Player()); 
     this.players.Add(new Player()); 
     this.players.Add(new Player()); 
     this.players.Add(new Player()); 
     this.players.Add(new Player()); 

     this.Players.ItemsSource = players; 
    } 

Player.cs:

public class Player : INotifyPropertyChanged 
{ 
    private string name; 

    public string Name 
    { 
     get { return name; } 
     set 
     { 
      if (value == name) return; 
      name = value; 
      OnPropertyChanged(); 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    [NotifyPropertyChangedInvocator] 
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) 
    { 
     PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 

La compilazione e l'applicazione funziona e fornisce il comportamento previsto:

Working app