2017-05-17 9 views
0

XAMLのバインドに問題がありますが、自分では解決できません。私にはListViewComboBoxがあります。 ListViewItemsSourceは、TabControlのビューモデルにあり、ComboBox.ItemsSourceもあります。このコレクションにComboBoxをバインドするにはどうすればよいですか?ここでコンボボックスをタブのプロパティでListViewにバインドする

は、これまでのところ、私のコードです:

<ListView ItemsSource="{Binding ListViewSource}" SelectedItem="{Binding ListViewSelection}" 
      ItemTemplate="{DynamicResource ListViewTemplate}"> 
    <ListView.View> 
     <GridView> 
      <GridViewColumn Header="..."> 
       <GridViewColumn.CellTemplate> 
        <DataTemplate> 
         <ComboBox ItemsSource="{Binding ComboBoxSource}" 
            SelectedValue="{Binding ...}" 
            DisplayMemberPath="DisplayName"/> 
        </DataTemplate> 
       </GridViewColumn.CellTemplate> 
      </GridViewColumn> // ... 

そして、ここではTabViewModelItemsSource性質のちょうど頭です:

public ObservableCollection<TypeViewModel> ComboBoxSource { get; set; } 
public ObservableCollection<CostViewModel> ListViewSource { get; set; } 

それは、このプロパティにCombBox.ItemsSourceをバインドすることは可能ですか?

+3

との結合、windowに名前を与える: 'のItemsSource =」 {DataContext.ComboBoxSource、RelativeSource = {RelativeSource AncestorType = ListView}}をバインドします。 –

+2

相対的なソースは、前のコメントごとに、あなたが望むことを行うことができます。残念ながら、あなたの質問は 'ListView'のすべての項目が同じドロップダウンを持つ理由を明確にしていませんが、何らかの理由で相対的な方法があなたのニーズに合わない場合、それぞれの' TypeViewModel親ビューモデル(例えば 'ComboBoxSource {get {return _parentViewModel.ComboBoxSource;}}')に委譲する独自の 'ComboBoxSource'プロパティを公開するか、これらの値の起源に応じて、おそらくシングルトンにします。 –

+0

残念なことに、これは私のためにはうまくいかなかったが、私はKMChoが答えのセクション{Binding ElementName = ControlName、Path = DataContext.ComboBoxSourceでそれを説明した方法を今解決した。 } – stl

答えて

0

ただ1つの例です。 あなたはTabViewModelのようなものを持っています。

public class TabViewModel 
{ 
    public ObservableCollection<CostViewModel> ListViewSource { get; set; } 
    public ObservableCollection<TypeViewModel> ComboBoxSource { get; set; } 

    public TabViewModel() 
    { 
     ListViewSource = new ObservableCollection<CostViewModel>(); 
     ListViewSource.Add(new CostViewModel() { CostA = "A", CostB = "B" }); 
     ListViewSource.Add(new CostViewModel() { CostA = "1", CostB = "2" });    

     ComboBoxSource = new ObservableCollection<TypeViewModel>(); 
     ComboBoxSource.Add(new TypeViewModel() { TypeA = "A1", TypeB = "B1" }); 
     ComboBoxSource.Add(new TypeViewModel() { TypeA = "A2", TypeB = "B2" }); 
    } 
} 

とあなたのwindow

TabViewModel vm { get; set; }   
public Window1() 
{ 
    vm = new TabViewModel(); 
    this.DataContext = vm;    
    InitializeComponent(); 
} 

DataContextを入れて、推測では、CellTemplateでElementName

<Window x:Class="WpfApplication5.Window1" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="Window1" Height="300" Width="300" Name="mainWnd">  
    <Grid>   
     <ListView ItemsSource="{Binding ListViewSource}" > 
      <ListView.View> 
       <GridView> 
        <GridViewColumn Header="..."> 
         <GridViewColumn.CellTemplate> 
          <DataTemplate> 
           <ComboBox ItemsSource="{Binding ElementName=mainWnd, Path=DataContext.ComboBoxSource}" DisplayMemberPath="TypeA"/> 
          </DataTemplate> 
         </GridViewColumn.CellTemplate> 
        </GridViewColumn> 
       </GridView> 
      </ListView.View> 
     </ListView>   
    </Grid> 
</Window> 
+0

大きな事例をありがとうございました!これは私の問題を解決しました。 – stl

関連する問題