2017-05-31 9 views
-2

私はすべての場所を読んでいます。そのバインディングはWPF to Interfacesで実行できますが、実際には何かの牽引力を得ています。私はあなたが私のコードを準備するのに役立つなら、EFコアも使用しています。 ComboBoxはデータで塗りつぶされ、データのバインドは機能しますが、SelectedItemはバインドに失敗し、選択した項目内のテキストは空白になります。ComboBox SelectedItemのためのWPFのバインドインターフェイス

私は次のことを理解していない、インターフェイスを実装するオブジェクトにバインドします。

コンボボックスのためのXAML:

<ComboBox Height="23" x:Name="cbJumpList" Width="177" Margin="2" HorizontalAlignment="Left" 
      IsEditable="False" 
      DisplayMemberPath="Name" 
      SelectedItem="{Binding Path=(model:IData.SelectedJumpList), Mode=TwoWay}" 
      /> 

MainWindow.xaml.cs:

protected IData DB { get; private set; } 
public MainWindow() 
{ 
    InitializeComponent(); 

    DB = new Data.DataSQLite(true); 
    DB.Bind_JumpLists_ItemsSource(cbJumpList); 
} 

IData.cs:

public interface IData : IDisposable, INotifyPropertyChanged 
{ 
    void Bind_JumpLists_ItemsSource(ItemsControl control); 
    IJumpList First_JumpList(); 

    IJumpList SelectedJumpList { get; set; } // TwoWay Binding 
} 

IJumpList.cs

は、次に実装されたオブジェクト(Data.DataSQLite)内:

public void Bind_JumpLists_ItemsSource(ItemsControl control) 
{ 
    control.ItemsSource = null; 

    db.JumpLists.ToList(); 
    control.ItemsSource = db.JumpLists.Local; 
    control.Tag = db.JumpLists.Local; 

    SelectedJumpList = db.JumpLists.FirstOrDefault(); 
} 

public IJumpList SelectedJumpList 
{ 
    get { return _SelectedJumpList; } 
    set 
    { 
     _SelectedJumpList = value; 
     NotifyPropertyChanged(); 
    } 
} 
IJumpList _SelectedJumpList; 

private void NotifyPropertyChanged([CallerMemberName] string propertyName = "") 
{ 
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
} 

私はPropertyChangedイベントがnullのまま、追加する必要があります。

+0

は、ToListメソッド() ''から返されたプロパティをのSelectedItemするリストをバインドしようとすると? 'IJumpList'の中身は何ですか? –

+1

@SivaGopalいいえ、ToListは別のバインディングで使用されている 'Items.Local'プロパティを別の場所で更新しています。私はそれが混乱の原因になるかもしれないと思った、私はそれを編集します。 –

答えて

0

ComboBoxSelectedItemプロパティはプロパティにバインドされており、型にはバインドされていないものとします。バインディングを有効にするには、ComboBoxDataContextを、このプロパティが定義されている型のインスタンスに設定する必要があります。

これを試してみてください:

<ComboBox Height="23" x:Name="cbJumpList" Width="177" Margin="2" HorizontalAlignment="Left" 
      IsEditable="False" 
      DisplayMemberPath="Name" 
      SelectedItem="{Binding SelectedJumpList}" /> 

public void Bind_JumpLists_ItemsSource(ItemsControl control) 
{ 
    db.JumpLists.ToList(); 
    control.DataContext = this; 
    control.ItemsSource = db.JumpLists.Local; 
    control.Tag = db.JumpLists.Local; 

    SelectedJumpList = db.JumpLists.FirstOrDefault(); 
} 
+1

mm8ありがとうございます。型のインスタンス上の 'control.DataContext = this;'がキーでした。 SelectedItemの変更は関係ありませんでした。あなたのやり方はうまくいったし、私のものもそうだった。今私はそれを変更することが影響を与える私の他のリストを更新するために取得する必要がありますが、これは大きなバンプです。それが解決策であるので、誰かがそれを落としたと信じられない。 –

関連する問題