2017-01-20 2 views
0

List<Item> Items 사용자 지정 개체로 표시되는 ListBox이 있습니다. Foreach 항목 사용자가 ComboBoxList<string> Options으로 표시하고 선택한 Item이 Item의 속성으로 다시 연결되도록합니다. 목록 상자에서 개별 항목 속성을 바인딩하는 데 문제가 없지만 DataContext에 백업하여 옵션 목록을 가져 오는 방법은 무엇입니까?DataContext ComboBox ListBox 내부 바인딩

보기 모델은 내가 많은 추가 코드를 잘라 읽을 수있는 예에 도착하려고

<ListBox x:Name="ItemsListBox" ItemsSource="{Binding Items}"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <Grid Height="50" > 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="*"/> 
        <ColumnDefinition Width="*"/> 
       </Grid.ColumnDefinitions> 
       <TextBlock x:Name="ItemProperty1TB" 
        Text="{Binding Path=Property1, Mode=OneWay}" 
        Grid.Column="0" 
       /> 
       <ComboBox x:Name="OptionsCB" 
        SelectedItem ="{Binding ChosenOptions, Mode=TwoWay}" 
        ItemsSource="{Binding Path=DataContext.Options}"   
        Grid.Column="1" 
        PlaceholderText="Option" 
       /> 
      </Grid> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

페이지의 DataContext에

class ViewModel 
{ 
     public ObservableCollection<string> Options { get; set; } 
     public ObservableCollection<Item> Items { get; set; } 
} 

XAML로 설정되고있다.

How to bind to a source inside a ListBox different from the ItemsSource already specified 이것은 존재하지 않는 AncestorType을 사용합니까?

ComboBox inside Listbox.ItemTemplate Binding problem 정적 리소스에 바인딩됩니다. 내 옵션을 정적 리소스에 넣어야합니까?

ElementName 유망 보이지만 내 IDE는 요소가 목록 상자 내부에 범위 권장합니다 ... 이 VISUAL STUDIO

을 신뢰하지 않는 난 그냥이 모든 잘못에 대해 건가?

+0

뷰 모델은 속성이 아닌 필드를 가질 필요가있다. 그게 실제 코드인가요? –

+0

실제 코드가 아니므로 그 부분을 빨리 바꿀 것입니다. – JaredStroeb

+1

'ItemsSource = "{Binding Path = DataContext.Options, ElementName = ItemsListBox}"를 시도하십시오 " –

답변

0

이 시도 :

ItemsSource="{Binding Path=DataContext.Options, ElementName=ItemsListBox}" 
0

콤보 박스 바인딩 개체에 RelativeSource 속성을 사용하여 부모를 찾을 수 있습니다. 이 같은 것이 작동해야합니다.

ItemsSource="{Binding Path=DataContext.Options, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" 

Page 또는 Window를 사용하는 경우 UserControl을 페이지로 바꾸십시오.

+0

그 방법은 WPF에서는 작동하지만 Windows 전화에서는 작동하지 않는 것으로 보입니다. – JaredStroeb

+0

OK Windows phone에 대해서는 잘 모르겠지만 wp에서 작동하는 경우 부모에게 ElementName을 사용할 수 있습니다. –