2011-09-09 6 views
1

私はしたい独自の動作があります。Silverlight SelectedIndexデータバインディングが正しくありません

私はviewmodelアイテムのリストにデータバインドされたコンボボックスを持っています。最初の項目は「[項目の選択]」です。予想される動作は、ユーザーが項目を選択すると、何かをしてから最初の項目にインデックスを戻します。

3番目の項目を2回連続して選択する場合を除いて、これは機能します。ここでは、コードです: ItemViewModel:

// NOTE, I have all the INotify goo actually implemented, this is the shorthand 
// without goo to make it more readable. 
public class ItemViewModel : INotifyPropertyChanged 
{ 
    public string Caption { get; set; } 
    public string Test { get; set; } 
} 

のViewModel:(私のすべてのプロパティを適宜セットにOnPropertyChangedを呼び出す)

public class ViewModel : INotifyPropertyChanged 
{ 
    public ObservableCollection<ItemViewModel> ChildItems { get; set; } 
    public int SelectedChildIndex { get; set; } 
    public string DebugOutText { get; set; } 

    public ViewModel() 
    { 
    ChildItems = new ObservableCollection<ItemViewModel>(); 
    SelectedChildIndex = -1; 
    DebugOutText = string.Empty; 
    } 
    public void LoadChildItems() 
    { 
    ChildItems.Add(new ItemViewModel { Caption = "[ Select Item ]" }); 
    ChildItems.Add(new ItemViewModel { Caption = "One", Test = "Item 1" }); 
    ChildItems.Add(new ItemViewModel { Caption = "Two", Test = "Item 2" }); 
    ChildItems.Add(new ItemViewModel { Caption = "Three", Test = "Item 3" }); 
    SelectedChildIndex = 0; 
    } 
    private void OnPropertyChanged(string propName) 
    { 
    if (propName == "SelectedChildIndex") { this.OnSelectedChildIndexChanged(); } 
    if (this.PropertyChanged != null) 
    { this.PropertyChanged(this, new PropertyChangedEventArgs(propName)); } 
    } 
    public void OnSelectedChildIndexChanged() 
    { 
    if (SelectedChildIndex <= 0) return; 
    DebugOutText += "\r\n" + ChildItems[SelectedChildIndex].Test; 
    SelectedChildIndex = 0; // <- BIG ISSUE HERE 
    } 
} 

今私のXAML:

<StackPanel HorizontalAlignment="Left"> 
    <ComboBox Width="200" x:Name="combo" 
      ItemsSource="{Binding Path=ChildItems}" 
      SelectedIndex="{Binding Path=SelectedChildIndex, Mode=TwoWay}" 
      DisplayMemberPath="Caption" /> 
    <TextBlock Text="{Binding Path=DebugOutText}"/> 
</StackPanel> 

最後に私のアプリの起動:

var vm = new ViewModel(); 
vm.LoadChildItems(); 
this.RootVisual = new MainPage { DataContext = vm }; 

レポ手順は次のとおりです。

  • を実行し、それ
  • は「二」を選択/コンボを選択し、クリックしてください。
  • ここで、コンボをクリックします。ビジュアルスタイルでは、「2つ」が強調表示されていることが示されます(「[項目の選択]」を強調表示する必要があります)。 「2」をクリック/選択すると、何も起こりません。

は、私が行って、いくつかのトレースコードを入れている、コンボのselectedIndexはViewModel.SelectedChildIndexが0で、0であるが、私は何か他のものを選択しない限り、コンボのSelectionChangedは、発生しません。

私はそれをうまく動作させる方法がわかりません。どんな助けもありがとう。

答えて

1

私はあなたが望む行動の使い勝手について疑問を持っていることを認めなければなりません。あなたは、この目的のためにコンボボックスを使用する必要がある場合はそれにもかかわらず、私はこれで問題が解決しないと思う

Deployment.Current.Dispatcher.BeginInvoke(() => SelectedChildIndex = 0); 
+0

それでした。ありがとう。実際の目的は、コードスニペットを選択してテキストの本文に自動的に挿入し、クリックを保存して挿入を行う特別なボタンを持たないようにすることです。再度、感謝します。 – Andre

0

ここで野生の推測をするつもりです。 :)

// NOTE, I have all the INotify goo actually implemented, this is the shorthand 
// without goo to make it more readable. 

あなたはINotifyPropertyChangedの実装を表示しません。

短絡
public int SelectedChildIndex 
{ 
    get 
    { 
     return _selectedChildIndex; 
    } 
    set 
    { 
     if (value == _selectedChildIndex) return; 
     _selectedChildIndex= value; 
     OnProperyChanged("SelectedChildIndex "); 
    } 
} 

プロパティの値が変更されない場合に通知:ほとんどの実装は次のように何かをします。あなたのコードがこのように見える場合は、 "if"ステートメントを取り出し、それが修正されているかどうかを確認します。

+0

とライン

SelectedChildIndex = 0; 

を交換してみてください。私はAndreのコードをとり、そのような 'if'ステートメントを使わずに'Inotify goo 'を実装し、Andreが説明したとおりに正しく動作しました。 –

+0

@phil ifを使ってコードを作成しましたが、削除しても機能しませんでした。しかし、提案をありがとう。 – Andre

関連する問題