DataTemplate 내부의 Comobox로 ItemSource를 동적으로 설정하는 방법은 무엇입니까?

StackOverflow https://stackoverflow.com/questions/1216406

  •  06-07-2019
  •  | 
  •  

문제

내가 하나가 Listbox 그리고 하나를 적용했습니다 DataTemplate 이와 같이

<ListBox>
   <ListBox.ItemTemplate>
<Grid>
 <TextBlock Text="{Binding Path=Name}" Grid.Row=0/>
    <ComoboBox Name="test"
            DisplayMemberPath="Country"
            SelectedValuePath="Country_ID">
</Grid>

  1. 어떻게로드 할 것인가 ItemSource 이에 ComboBox 동적으로 선택된 각 항목을 기반으로합니다 ListBox? WPF에 새로운 iam ... pls는 귀중한 제안을 도와줍니다.
도움이 되었습니까?

해결책

<ListBox>
  <ListBox.ItemTemplate>
   <Grid>
     <TextBlock Text="{Binding Path=Name}" Grid.Row=0/>
        <ComoboBox Name="test" 
           DataContent="{Binding RelativeSource={RelativeSource AncestorType=ListBox}}"                 
           ItemsSource="{Binding}"
        DisplayMemberPath="Country"
        SelectedValuePath="Country_ID">
   </Grid>

이제 Combocbox는 항상 Parent Listbox와 동일한 ItemsSource를 갖습니다.

다른 팁

이를 수행하는 한 가지 방법은 Combobox의 항목 소스를 Listbox의 선택된 값 속성에 바인딩하는 것입니다. 이를 위해서는 ListBox가 ComboBox가 바인딩 할 항목 목록이 포함 된 항목 모음에 묶여 있어야합니다.

<ListBox
    x:Name="CategoryList"
    ItemsSource="{Binding Path=MasterList, 
        RelativeSource={RelativeSource AncestorType=Window}}"
    DisplayMemberPath="MasterProperty"
    SelectedValuePath="Details"
    />

<ComboBox
    ItemsSource="{Binding Path=SelectedValue, ElementName=CategoryList}"
    DisplayMemberPath="DetailProperty"
    Grid.Row="1"
    />

이 예에서는 세부 사항 컬렉션이 포함 된 개체 목록을 노출시키는 창 뒤의 코드에서 공개 속성을 만들었습니다.

public List<Master> MasterList { get; set; }

public class Master
{
    public string MasterProperty { get; set; }
    public List<Detail> Details { get; set; }
}

public class Detail
{
    public string DetailProperty { get; set; }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top