2014-09-06 2 views
1

항목이 다른 범주로 정렬되는 listview를 만들었습니다. 그리고 Viewview의 selectedItem을 내 ViewModel의 속성으로 가져 오려고 노력하면서, 나는 단순히 SelectedItem 속성에 대한 바인딩을 설정하는 일반적인 방법을 따랐습니다.listview에서 selecteditems 속성을 가져 오는 방법

그러나 지금은 실제로 observable 컬렉션에 datacontext를 설정하고 ItemsSource 속성 (또는 내가 무엇입니까?)을 건드리지 않고 있기 때문에 Implete가 SelectedItem 작업에 대한 바인딩을 만들기위한 멋진 xaml 코드를 놓치고 있다고 생각합니다. 다행히 누군가가 비슷한 문제가 발생했습니다. xaml 코드이고 viewmodel에 선언 된 2 개의 속성이 아래에 표시됩니다.

<UserControl.Resources> 
    <CollectionViewSource x:Key="TileChangeType"        
        Source="{Binding TileChangeList.TileChangeListEntries}"> 
     <CollectionViewSource.GroupDescriptions> 
      <PropertyGroupDescription PropertyName="Type" /> 
     </CollectionViewSource.GroupDescriptions> 
    </CollectionViewSource> 
</UserControl.Resources> 
<ListView Grid.Column="0" x:Name="gridTiles" VirtualizingPanel.IsVirtualizing="False" VirtualizingPanel.IsVirtualizingWhenGrouping="False" 
         DataContext="{StaticResource TileChangeType}" 
         SelectedItem="{Binding SelectedTileChange}" 
     ItemsSource="{Binding IsAsync=True}"> 


      <ListView.GroupStyle> 
       <GroupStyle> 
        <GroupStyle.ContainerStyle> 
         <Style TargetType="{x:Type GroupItem}"> 
          <Setter Property="Template"> 
           <Setter.Value> 
            <ControlTemplate TargetType="{x:Type GroupItem}"> 
             <Expander IsExpanded="True"> 
              <Expander.Header> 
               <TextBlock Background="Aqua" Text="{Binding Path=Name}"/> 
              </Expander.Header> 
              <ItemsPresenter /> 
             </Expander> 
            </ControlTemplate> 
           </Setter.Value> 
          </Setter> 
         </Style> 
        </GroupStyle.ContainerStyle> 
        <GroupStyle.Panel> 
         <ItemsPanelTemplate> 
          <DataGridRowsPresenter/> 
         </ItemsPanelTemplate> 
        </GroupStyle.Panel> 
       </GroupStyle> 
      </ListView.GroupStyle> 
      <ListView.View> 
       <GridView> 
        <GridView.Columns> 
         <GridViewColumn Header="X" DisplayMemberBinding="{Binding X}" /> 
         <GridViewColumn Header="Y" DisplayMemberBinding="{Binding Y}" /> 
         <GridViewColumn Header="Z" DisplayMemberBinding="{Binding Z}" /> 
         <GridViewColumn Header="Type" DisplayMemberBinding="{Binding Type}" Width="40"/> 
        </GridView.Columns> 
       </GridView> 
      </ListView.View> 

      </ListView> 

모델 "SelectedTileChange"

는 다음과 같은 선언 :

public TileChange SelectedTileChange 
    { 
     get; 
     set; 
    } 

    //Class contains an Observable Collection of the TileChange class (same class as SelectedItem is binding to) 
    //E.g. ObservableCollection<TileChange> tileChangeListEntries; 
    public TileChangeList TileChangeList 
    { 
     get; 
     set; 
    } 

답변

1

SelectedTileChange 속성을 포함하는 뷰 모델로 설정되어 UserControl을의 데이터 컨텍스트를 가정하면,이 같은 SelectedItem 속성에 바인딩 할 수 있습니다 :

SelectedItem="{Binding DataContext.SelectedTileChange, 
       RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" 

이 해당 UserControl을 찾아, 그 데이터 컨텍스트에 SelectedTileChange 속성에 바인딩합니다.

그러나 일반적으로 ListView을 데이터 액세스 텍스트를 설정하는 대신 CollectionViewSource에 직접 바인딩하면됩니다. 그런 식으로 RelativeSource 바인딩을 만들 필요가 없습니다.

<ListView ItemsSource="{Binding Source={StaticResource TileChangeType}}" 
      SelectedItem="{Binding SelectedTileChange}" /> 
+0

우수 답변! 당신은 하나의 문제 2 개를 방금 해결했습니다! 나는 접두사 DataContext를 사용하지 않았기 때문에, 요소를 제외하고 상대 소스를 사용하여 어떤 것을 묶어 본 적이 없다. ..., (대신 datacontext 전체 요소를 설정해야했다). 그리고 지금이 작품! Btw는 "listview를 collectionviewsource에 직접 바인딩"하는 예를 보여줍니다. – LamaCoder

+0

@ ETG87 잘 듣는 것이 좋다. collectionviewsource에 직접 바인딩하는 방법을 보여주기 위해 몇 가지 코드를 추가했습니다. –

+0

참 아주 좋은! – LamaCoder

관련 문제