문제

3 개의 테이블이 있습니다 : 항목 - 데이터 콘텍스트 - 내비게이션 열 그룹이 있습니다. 내비게이션 열 범주가 있습니다.

Datagrid (카테고리 및 그룹) 열 모두를 원하고 카테고리를 선택하면 COL COL에만 표시해야합니다.

다음은 다음과 같습니다.

<tk:DataGrid AutoGenerateColumns="False" ItemsSource="{Binding}">
    <tk:DataGrid.Columns>

        <!--Works-->
        <tk:DataGridComboBoxColumn                                        
            Header="Categroy" 
            DisplayMemberPath="Title"                    
            SelectedValuePath="CategoryId"
            SelectedValueBinding="{Binding Group.Category.CategoryId}"
            ItemsSource="{Binding Context.Categories, 
                Source={x:Static Application.Current}}"
        />


        <!--Look at these two things:-->

        <!--This does work-->
        <tk:DataGridTemplateColumn>
            <tk:DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <ItemsControl
                        ItemsSource="{Binding Group.Category.Groups}">
                        <ItemsControl.ItemTemplate>
                            <DataTemplate DataType="{x:Type data:Group}">
                                <TextBlock Text="{Binding Title}"/>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </DataTemplate>
            </tk:DataGridTemplateColumn.CellTemplate>
        </tk:DataGridTemplateColumn>

        <!--But this does NOT work, even it's the same source-->
        <!--Notice I even tried a dummy converter and doesnt reach there-->
        <tk:DataGridComboBoxColumn 
            Header="Group" 
            DisplayMemberPath="Title"
            SelectedValuePath="GroupId"
            ItemsSource="{Binding Group.Category.Groups,
                Converter={StaticResource DummyConverter}}"
            SelectedValueBinding="{Binding Group.GroupId}"
            />

    </tk:DataGrid.Columns>
</tk:DataGrid>

업데이트
문제는 항목 소스 속성이 비 정적 바인딩으로 설정할 수 없다는 것입니다. 나는 심지어 Itemssource를 설정했기 때문에 {Binding} 이랑 DummyConverter 변환기에서 멈추지 않습니다. 그리고 범주에서 Combobox에서는 잘 작동합니다.

도움이 되었습니까?

해결책

Datagrid의 열에는 시각적 트리에 추가되지 않기 때문에 DataContext가 없습니다. 조금 이상하게 들리지만 살펴보십시오 빈스의 블로그, 그것은 시각적 레이아웃의 좋은 예를 얻었습니다. 그리드가 그려지면 셀에 데이터 컨텍스트가 있고 정상 바인딩 (정적 자원이 아님)을 사용하여 콤보 박스 항목 소스를 설정할 수 있습니다.)

콤보 박스 항목 소스에 액세스 할 수 있습니다.

<dg:DataGridComboBoxColumn>
   <dg:DataGridComboBoxColumn.EditingElementStyle>
      <Style TargetType="ComboBox">
         <Setter Property="ItemsSource" Value="{Binding Path=MyBindingPath}" />
      </Style>
   </dg:DataGridComboBoxColumn.EditingElementStyle>
</dg:DataGridComboBoxColumn>

보세요 여기 그리고 또한 여기 일부 코드의 경우. 당신은 또한 필요합니다 비 편집 요소의 항목 소스를 설정하십시오 이것에서와 같이 게시하다

다른 팁

나는 mvvm을 사용하고 있었고 바인딩하고 싶었습니다. ItemSource window window 데이터 컨텍스트의 객체 모음으로 열의. 나는 10 가지 다른 방법을 시도했을 것입니다 아무것도 효과가 없었습니다 내가 찾을 때까지 이 답변.

속임수는 a를 정의하는 것입니다 CollectionViewSource 그리드 외부에서 그리드 내부에서 참조하십시오. StaticResource. 예를 들어,

<Window.Resources>
    <CollectionViewSource x:Key="ItemsCVS" Source="{Binding MyItems}" />
</Window.Resources>
<!-- ... -->
<DataGrid ItemsSource="{Binding MyRecords}">
    <DataGridComboBoxColumn Header="Column With Predefined Values"
                            ItemsSource="{Binding Source={StaticResource ItemsCVS}}"
                            SelectedValueBinding="{Binding MyItemId}"
                            SelectedValuePath="Id"
                            DisplayMemberPath="StatusCode" />
</DataGrid>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top